SDL.c 27 KB

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