2
0

SDL.c 25 KB

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