SDL.c 28 KB

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