SDL.c 25 KB

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