SDL.c 27 KB

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