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. if (!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. if (!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. /* Private helper to either increment's existing ref counter,
  211. * or fully init a new subsystem. */
  212. static bool SDL_InitOrIncrementSubsystem(Uint32 subsystem)
  213. {
  214. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  215. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  216. if (subsystem_index < 0) {
  217. return false;
  218. }
  219. if (SDL_SubsystemRefCount[subsystem_index] > 0) {
  220. ++SDL_SubsystemRefCount[subsystem_index];
  221. return true;
  222. }
  223. return SDL_InitSubSystem(subsystem);
  224. }
  225. void SDL_SetMainReady(void)
  226. {
  227. SDL_MainIsReady = true;
  228. if (SDL_MainThreadID == 0) {
  229. SDL_MainThreadID = SDL_GetCurrentThreadID();
  230. }
  231. }
  232. bool SDL_IsMainThread(void)
  233. {
  234. if (SDL_MainThreadID == 0) {
  235. // Not initialized yet?
  236. return true;
  237. }
  238. if (SDL_MainThreadID == SDL_GetCurrentThreadID()) {
  239. return true;
  240. }
  241. return false;
  242. }
  243. // Initialize all the subsystems that require initialization before threads start
  244. void SDL_InitMainThread(void)
  245. {
  246. static bool done_info = false;
  247. SDL_InitTLSData();
  248. SDL_InitEnvironment();
  249. SDL_InitTicks();
  250. SDL_InitFilesystem();
  251. if (!done_info) {
  252. const char *value;
  253. value = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
  254. SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "App name: %s", value ? value : "<unspecified>");
  255. value = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING);
  256. SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "App version: %s", value ? value : "<unspecified>");
  257. value = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING);
  258. SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "App ID: %s", value ? value : "<unspecified>");
  259. SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "SDL revision: %s", SDL_REVISION);
  260. done_info = true;
  261. }
  262. }
  263. static void SDL_QuitMainThread(void)
  264. {
  265. SDL_QuitFilesystem();
  266. SDL_QuitTicks();
  267. SDL_QuitEnvironment();
  268. SDL_QuitTLSData();
  269. }
  270. bool SDL_InitSubSystem(SDL_InitFlags flags)
  271. {
  272. Uint32 flags_initialized = 0;
  273. if (!SDL_MainIsReady) {
  274. return SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
  275. }
  276. SDL_InitMainThread();
  277. #ifdef SDL_USE_LIBDBUS
  278. SDL_DBus_Init();
  279. #endif
  280. #ifdef SDL_PLATFORM_WINDOWS
  281. if (flags & (SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK)) {
  282. if (!SDL_HelperWindowCreate()) {
  283. goto quit_and_error;
  284. }
  285. }
  286. #endif
  287. // Initialize the event subsystem
  288. if (flags & SDL_INIT_EVENTS) {
  289. if (SDL_ShouldInitSubsystem(SDL_INIT_EVENTS)) {
  290. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  291. if (!SDL_InitEvents()) {
  292. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  293. goto quit_and_error;
  294. }
  295. } else {
  296. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  297. }
  298. flags_initialized |= SDL_INIT_EVENTS;
  299. }
  300. // Initialize the video subsystem
  301. if (flags & SDL_INIT_VIDEO) {
  302. #ifndef SDL_VIDEO_DISABLED
  303. if (SDL_ShouldInitSubsystem(SDL_INIT_VIDEO)) {
  304. // video implies events
  305. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  306. goto quit_and_error;
  307. }
  308. // We initialize video on the main thread
  309. // On Apple platforms this is a requirement.
  310. // On other platforms, this is the definition.
  311. SDL_MainThreadID = SDL_GetCurrentThreadID();
  312. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  313. if (!SDL_VideoInit(NULL)) {
  314. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  315. SDL_PushError();
  316. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  317. SDL_PopError();
  318. goto quit_and_error;
  319. }
  320. } else {
  321. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  322. }
  323. flags_initialized |= SDL_INIT_VIDEO;
  324. #else
  325. SDL_SetError("SDL not built with video support");
  326. goto quit_and_error;
  327. #endif
  328. }
  329. // Initialize the audio subsystem
  330. if (flags & SDL_INIT_AUDIO) {
  331. #ifndef SDL_AUDIO_DISABLED
  332. if (SDL_ShouldInitSubsystem(SDL_INIT_AUDIO)) {
  333. // audio implies events
  334. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  335. goto quit_and_error;
  336. }
  337. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  338. if (!SDL_InitAudio(NULL)) {
  339. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  340. SDL_PushError();
  341. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  342. SDL_PopError();
  343. goto quit_and_error;
  344. }
  345. } else {
  346. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  347. }
  348. flags_initialized |= SDL_INIT_AUDIO;
  349. #else
  350. SDL_SetError("SDL not built with audio support");
  351. goto quit_and_error;
  352. #endif
  353. }
  354. // Initialize the joystick subsystem
  355. if (flags & SDL_INIT_JOYSTICK) {
  356. #ifndef SDL_JOYSTICK_DISABLED
  357. if (SDL_ShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
  358. // joystick implies events
  359. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  360. goto quit_and_error;
  361. }
  362. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  363. if (!SDL_InitJoysticks()) {
  364. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  365. SDL_PushError();
  366. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  367. SDL_PopError();
  368. goto quit_and_error;
  369. }
  370. } else {
  371. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  372. }
  373. flags_initialized |= SDL_INIT_JOYSTICK;
  374. #else
  375. SDL_SetError("SDL not built with joystick support");
  376. goto quit_and_error;
  377. #endif
  378. }
  379. if (flags & SDL_INIT_GAMEPAD) {
  380. #ifndef SDL_JOYSTICK_DISABLED
  381. if (SDL_ShouldInitSubsystem(SDL_INIT_GAMEPAD)) {
  382. // game controller implies joystick
  383. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_JOYSTICK)) {
  384. goto quit_and_error;
  385. }
  386. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  387. if (!SDL_InitGamepads()) {
  388. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  389. SDL_PushError();
  390. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  391. SDL_PopError();
  392. goto quit_and_error;
  393. }
  394. } else {
  395. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  396. }
  397. flags_initialized |= SDL_INIT_GAMEPAD;
  398. #else
  399. SDL_SetError("SDL not built with joystick support");
  400. goto quit_and_error;
  401. #endif
  402. }
  403. // Initialize the haptic subsystem
  404. if (flags & SDL_INIT_HAPTIC) {
  405. #ifndef SDL_HAPTIC_DISABLED
  406. if (SDL_ShouldInitSubsystem(SDL_INIT_HAPTIC)) {
  407. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  408. if (!SDL_InitHaptics()) {
  409. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  410. goto quit_and_error;
  411. }
  412. } else {
  413. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  414. }
  415. flags_initialized |= SDL_INIT_HAPTIC;
  416. #else
  417. SDL_SetError("SDL not built with haptic (force feedback) support");
  418. goto quit_and_error;
  419. #endif
  420. }
  421. // Initialize the sensor subsystem
  422. if (flags & SDL_INIT_SENSOR) {
  423. #ifndef SDL_SENSOR_DISABLED
  424. if (SDL_ShouldInitSubsystem(SDL_INIT_SENSOR)) {
  425. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  426. if (!SDL_InitSensors()) {
  427. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  428. goto quit_and_error;
  429. }
  430. } else {
  431. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  432. }
  433. flags_initialized |= SDL_INIT_SENSOR;
  434. #else
  435. SDL_SetError("SDL not built with sensor support");
  436. goto quit_and_error;
  437. #endif
  438. }
  439. // Initialize the camera subsystem
  440. if (flags & SDL_INIT_CAMERA) {
  441. #ifndef SDL_CAMERA_DISABLED
  442. if (SDL_ShouldInitSubsystem(SDL_INIT_CAMERA)) {
  443. // camera implies events
  444. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  445. goto quit_and_error;
  446. }
  447. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  448. if (!SDL_CameraInit(NULL)) {
  449. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  450. SDL_PushError();
  451. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  452. SDL_PopError();
  453. goto quit_and_error;
  454. }
  455. } else {
  456. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  457. }
  458. flags_initialized |= SDL_INIT_CAMERA;
  459. #else
  460. SDL_SetError("SDL not built with camera support");
  461. goto quit_and_error;
  462. #endif
  463. }
  464. (void)flags_initialized; // make static analysis happy, since this only gets used in error cases.
  465. return SDL_ClearError();
  466. quit_and_error:
  467. {
  468. SDL_PushError();
  469. SDL_QuitSubSystem(flags_initialized);
  470. SDL_PopError();
  471. }
  472. return false;
  473. }
  474. bool SDL_Init(SDL_InitFlags flags)
  475. {
  476. return SDL_InitSubSystem(flags);
  477. }
  478. void SDL_QuitSubSystem(SDL_InitFlags flags)
  479. {
  480. // Shut down requested initialized subsystems
  481. #ifndef SDL_CAMERA_DISABLED
  482. if (flags & SDL_INIT_CAMERA) {
  483. if (SDL_ShouldQuitSubsystem(SDL_INIT_CAMERA)) {
  484. SDL_QuitCamera();
  485. // camera implies events
  486. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  487. }
  488. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  489. }
  490. #endif
  491. #ifndef SDL_SENSOR_DISABLED
  492. if (flags & SDL_INIT_SENSOR) {
  493. if (SDL_ShouldQuitSubsystem(SDL_INIT_SENSOR)) {
  494. SDL_QuitSensors();
  495. }
  496. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  497. }
  498. #endif
  499. #ifndef SDL_JOYSTICK_DISABLED
  500. if (flags & SDL_INIT_GAMEPAD) {
  501. if (SDL_ShouldQuitSubsystem(SDL_INIT_GAMEPAD)) {
  502. SDL_QuitGamepads();
  503. // game controller implies joystick
  504. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  505. }
  506. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  507. }
  508. if (flags & SDL_INIT_JOYSTICK) {
  509. if (SDL_ShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  510. SDL_QuitJoysticks();
  511. // joystick implies events
  512. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  513. }
  514. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  515. }
  516. #endif
  517. #ifndef SDL_HAPTIC_DISABLED
  518. if (flags & SDL_INIT_HAPTIC) {
  519. if (SDL_ShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  520. SDL_QuitHaptics();
  521. }
  522. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  523. }
  524. #endif
  525. #ifndef SDL_AUDIO_DISABLED
  526. if (flags & SDL_INIT_AUDIO) {
  527. if (SDL_ShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  528. SDL_QuitAudio();
  529. // audio implies events
  530. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  531. }
  532. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  533. }
  534. #endif
  535. #ifndef SDL_VIDEO_DISABLED
  536. if (flags & SDL_INIT_VIDEO) {
  537. if (SDL_ShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  538. SDL_QuitRender();
  539. SDL_VideoQuit();
  540. // video implies events
  541. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  542. }
  543. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  544. }
  545. #endif
  546. if (flags & SDL_INIT_EVENTS) {
  547. if (SDL_ShouldQuitSubsystem(SDL_INIT_EVENTS)) {
  548. SDL_QuitEvents();
  549. }
  550. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  551. }
  552. }
  553. Uint32 SDL_WasInit(SDL_InitFlags flags)
  554. {
  555. int i;
  556. int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
  557. Uint32 initialized = 0;
  558. // Fast path for checking one flag
  559. if (SDL_HasExactlyOneBitSet32(flags)) {
  560. int subsystem_index = SDL_MostSignificantBitIndex32(flags);
  561. return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
  562. }
  563. if (!flags) {
  564. flags = SDL_INIT_EVERYTHING;
  565. }
  566. num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
  567. // Iterate over each bit in flags, and check the matching subsystem.
  568. for (i = 0; i < num_subsystems; ++i) {
  569. if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
  570. initialized |= (1 << i);
  571. }
  572. flags >>= 1;
  573. }
  574. return initialized;
  575. }
  576. void SDL_Quit(void)
  577. {
  578. SDL_bInMainQuit = true;
  579. // Quit all subsystems
  580. #ifdef SDL_PLATFORM_WINDOWS
  581. SDL_HelperWindowDestroy();
  582. #endif
  583. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  584. SDL_CleanupTrays();
  585. #ifdef SDL_USE_LIBDBUS
  586. SDL_DBus_Quit();
  587. #endif
  588. #if defined(SDL_PLATFORM_UNIX) && !defined(SDL_PLATFORM_ANDROID) && !defined(SDL_PLATFORM_EMSCRIPTEN)
  589. SDL_Gtk_Quit();
  590. #endif
  591. SDL_QuitTimers();
  592. SDL_QuitAsyncIO();
  593. SDL_SetObjectsInvalid();
  594. SDL_AssertionsQuit();
  595. SDL_QuitPixelFormatDetails();
  596. SDL_QuitCPUInfo();
  597. /* Now that every subsystem has been quit, we reset the subsystem refcount
  598. * and the list of initialized subsystems.
  599. */
  600. SDL_memset(SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount));
  601. SDL_QuitLog();
  602. SDL_QuitHints();
  603. SDL_QuitProperties();
  604. SDL_QuitMainThread();
  605. SDL_bInMainQuit = false;
  606. }
  607. // Get the library version number
  608. int SDL_GetVersion(void)
  609. {
  610. return SDL_VERSION;
  611. }
  612. // Get the library source revision
  613. const char *SDL_GetRevision(void)
  614. {
  615. return SDL_REVISION;
  616. }
  617. // Get the name of the platform
  618. const char *SDL_GetPlatform(void)
  619. {
  620. #if defined(SDL_PLATFORM_PRIVATE)
  621. return SDL_PLATFORM_PRIVATE_NAME;
  622. #elif defined(SDL_PLATFORM_AIX)
  623. return "AIX";
  624. #elif defined(SDL_PLATFORM_ANDROID)
  625. return "Android";
  626. #elif defined(SDL_PLATFORM_BSDI)
  627. return "BSDI";
  628. #elif defined(SDL_PLATFORM_EMSCRIPTEN)
  629. return "Emscripten";
  630. #elif defined(SDL_PLATFORM_FREEBSD)
  631. return "FreeBSD";
  632. #elif defined(SDL_PLATFORM_HAIKU)
  633. return "Haiku";
  634. #elif defined(SDL_PLATFORM_HPUX)
  635. return "HP-UX";
  636. #elif defined(SDL_PLATFORM_IRIX)
  637. return "Irix";
  638. #elif defined(SDL_PLATFORM_LINUX)
  639. return "Linux";
  640. #elif defined(__MINT__)
  641. return "Atari MiNT";
  642. #elif defined(SDL_PLATFORM_MACOS)
  643. return "macOS";
  644. #elif defined(SDL_PLATFORM_NETBSD)
  645. return "NetBSD";
  646. #elif defined(SDL_PLATFORM_NGAGE)
  647. return "Nokia N-Gage";
  648. #elif defined(SDL_PLATFORM_OPENBSD)
  649. return "OpenBSD";
  650. #elif defined(SDL_PLATFORM_OS2)
  651. return "OS/2";
  652. #elif defined(SDL_PLATFORM_OSF)
  653. return "OSF/1";
  654. #elif defined(SDL_PLATFORM_QNXNTO)
  655. return "QNX Neutrino";
  656. #elif defined(SDL_PLATFORM_RISCOS)
  657. return "RISC OS";
  658. #elif defined(SDL_PLATFORM_SOLARIS)
  659. return "Solaris";
  660. #elif defined(SDL_PLATFORM_WIN32)
  661. return "Windows";
  662. #elif defined(SDL_PLATFORM_WINGDK)
  663. return "WinGDK";
  664. #elif defined(SDL_PLATFORM_XBOXONE)
  665. return "Xbox One";
  666. #elif defined(SDL_PLATFORM_XBOXSERIES)
  667. return "Xbox Series X|S";
  668. #elif defined(SDL_PLATFORM_IOS)
  669. return "iOS";
  670. #elif defined(SDL_PLATFORM_TVOS)
  671. return "tvOS";
  672. #elif defined(SDL_PLATFORM_PS2)
  673. return "PlayStation 2";
  674. #elif defined(SDL_PLATFORM_PSP)
  675. return "PlayStation Portable";
  676. #elif defined(SDL_PLATFORM_VITA)
  677. return "PlayStation Vita";
  678. #elif defined(SDL_PLATFORM_3DS)
  679. return "Nintendo 3DS";
  680. #elif defined(SDL_PLATFORM_HURD)
  681. return "GNU/Hurd";
  682. #elif defined(__managarm__)
  683. return "Managarm";
  684. #else
  685. return "Unknown (see SDL_platform.h)";
  686. #endif
  687. }
  688. bool SDL_IsTablet(void)
  689. {
  690. #ifdef SDL_PLATFORM_ANDROID
  691. return SDL_IsAndroidTablet();
  692. #elif defined(SDL_PLATFORM_IOS)
  693. extern bool SDL_IsIPad(void);
  694. return SDL_IsIPad();
  695. #else
  696. return false;
  697. #endif
  698. }
  699. bool SDL_IsTV(void)
  700. {
  701. #ifdef SDL_PLATFORM_ANDROID
  702. return SDL_IsAndroidTV();
  703. #elif defined(SDL_PLATFORM_IOS)
  704. extern bool SDL_IsAppleTV(void);
  705. return SDL_IsAppleTV();
  706. #else
  707. return false;
  708. #endif
  709. }
  710. static SDL_Sandbox SDL_DetectSandbox(void)
  711. {
  712. #if defined(SDL_PLATFORM_LINUX)
  713. if (access("/.flatpak-info", F_OK) == 0) {
  714. return SDL_SANDBOX_FLATPAK;
  715. }
  716. /* For Snap, we check multiple variables because they might be set for
  717. * unrelated reasons. This is the same thing WebKitGTK does. */
  718. if (SDL_getenv("SNAP") && SDL_getenv("SNAP_NAME") && SDL_getenv("SNAP_REVISION")) {
  719. return SDL_SANDBOX_SNAP;
  720. }
  721. if (access("/run/host/container-manager", F_OK) == 0) {
  722. return SDL_SANDBOX_UNKNOWN_CONTAINER;
  723. }
  724. #elif defined(SDL_PLATFORM_MACOS)
  725. if (SDL_getenv("APP_SANDBOX_CONTAINER_ID")) {
  726. return SDL_SANDBOX_MACOS;
  727. }
  728. #endif
  729. return SDL_SANDBOX_NONE;
  730. }
  731. SDL_Sandbox SDL_GetSandbox(void)
  732. {
  733. static SDL_Sandbox sandbox;
  734. static bool sandbox_initialized;
  735. if (!sandbox_initialized) {
  736. sandbox = SDL_DetectSandbox();
  737. sandbox_initialized = true;
  738. }
  739. return sandbox;
  740. }
  741. #ifdef SDL_PLATFORM_WIN32
  742. #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
  743. // FIXME: Still need to include DllMain() on Watcom C ?
  744. BOOL APIENTRY MINGW32_FORCEALIGN _DllMainCRTStartup(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  745. {
  746. switch (ul_reason_for_call) {
  747. case DLL_PROCESS_ATTACH:
  748. case DLL_THREAD_ATTACH:
  749. case DLL_THREAD_DETACH:
  750. case DLL_PROCESS_DETACH:
  751. break;
  752. }
  753. return TRUE;
  754. }
  755. #endif // Building DLL
  756. #endif // defined(SDL_PLATFORM_WIN32)