SDL.c 27 KB

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