SDL.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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_WIN32) || defined(SDL_PLATFORM_GDK)
  21. #include "core/windows/SDL_windows.h"
  22. #elif !defined(SDL_PLATFORM_WINRT)
  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_log_c.h"
  33. #include "SDL_properties_c.h"
  34. #include "audio/SDL_sysaudio.h"
  35. #include "video/SDL_video_c.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. #define SDL_INIT_EVERYTHING ~0U
  42. /* Initialization/Cleanup routines */
  43. #include "timer/SDL_timer_c.h"
  44. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  45. extern int SDL_HelperWindowCreate(void);
  46. extern int SDL_HelperWindowDestroy(void);
  47. #endif
  48. #ifdef SDL_BUILD_MAJOR_VERSION
  49. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MAJOR_VERSION,
  50. SDL_MAJOR_VERSION == SDL_BUILD_MAJOR_VERSION);
  51. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MINOR_VERSION,
  52. SDL_MINOR_VERSION == SDL_BUILD_MINOR_VERSION);
  53. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MICRO_VERSION,
  54. SDL_PATCHLEVEL == SDL_BUILD_MICRO_VERSION);
  55. #endif
  56. SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_min, SDL_MAJOR_VERSION >= 0);
  57. /* Limited only by the need to fit in SDL_version */
  58. SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_max, SDL_MAJOR_VERSION <= 255);
  59. SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_min, SDL_MINOR_VERSION >= 0);
  60. /* Limited only by the need to fit in SDL_version */
  61. SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_max, SDL_MINOR_VERSION <= 255);
  62. SDL_COMPILE_TIME_ASSERT(SDL_PATCHLEVEL_min, SDL_PATCHLEVEL >= 0);
  63. /* Limited by its encoding in SDL_VERSIONNUM and in the ABI versions */
  64. SDL_COMPILE_TIME_ASSERT(SDL_PATCHLEVEL_max, SDL_PATCHLEVEL <= 99);
  65. /* This is not declared in any header, although it is shared between some
  66. parts of SDL, because we don't want anything calling it without an
  67. extremely good reason. */
  68. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  69. SDL_NORETURN void SDL_ExitProcess(int exitcode)
  70. {
  71. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  72. /* "if you do not know the state of all threads in your process, it is
  73. better to call TerminateProcess than ExitProcess"
  74. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx */
  75. TerminateProcess(GetCurrentProcess(), exitcode);
  76. /* MingW doesn't have TerminateProcess marked as noreturn, so add an
  77. ExitProcess here that will never be reached but make MingW happy. */
  78. ExitProcess(exitcode);
  79. #elif defined(SDL_PLATFORM_EMSCRIPTEN)
  80. emscripten_cancel_main_loop(); /* this should "kill" the app. */
  81. emscripten_force_exit(exitcode); /* this should "kill" the app. */
  82. exit(exitcode);
  83. #elif defined(SDL_PLATFORM_HAIKU) /* Haiku has _Exit, but it's not marked noreturn. */
  84. _exit(exitcode);
  85. #elif defined(HAVE__EXIT) /* Upper case _Exit() */
  86. _Exit(exitcode);
  87. #else
  88. _exit(exitcode);
  89. #endif
  90. }
  91. /* The initialized subsystems */
  92. #ifdef SDL_MAIN_NEEDED
  93. static SDL_bool SDL_MainIsReady = SDL_FALSE;
  94. #else
  95. static SDL_bool SDL_MainIsReady = SDL_TRUE;
  96. #endif
  97. static SDL_bool SDL_bInMainQuit = SDL_FALSE;
  98. static Uint8 SDL_SubsystemRefCount[32];
  99. /* Private helper to increment a subsystem's ref counter. */
  100. static void SDL_IncrementSubsystemRefCount(Uint32 subsystem)
  101. {
  102. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  103. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  104. if (subsystem_index >= 0) {
  105. ++SDL_SubsystemRefCount[subsystem_index];
  106. }
  107. }
  108. /* Private helper to decrement a subsystem's ref counter. */
  109. static void SDL_DecrementSubsystemRefCount(Uint32 subsystem)
  110. {
  111. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  112. if ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] > 0)) {
  113. --SDL_SubsystemRefCount[subsystem_index];
  114. }
  115. }
  116. /* Private helper to check if a system needs init. */
  117. static SDL_bool SDL_ShouldInitSubsystem(Uint32 subsystem)
  118. {
  119. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  120. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  121. return ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 0));
  122. }
  123. /* Private helper to check if a system needs to be quit. */
  124. static SDL_bool SDL_ShouldQuitSubsystem(Uint32 subsystem)
  125. {
  126. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  127. if ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 0)) {
  128. return SDL_FALSE;
  129. }
  130. /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
  131. * isn't zero.
  132. */
  133. return (((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 1)) || SDL_bInMainQuit);
  134. }
  135. /* Private helper to either increment's existing ref counter,
  136. * or fully init a new subsystem. */
  137. static SDL_bool SDL_InitOrIncrementSubsystem(Uint32 subsystem)
  138. {
  139. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  140. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  141. if (subsystem_index < 0) {
  142. return SDL_FALSE;
  143. }
  144. if (SDL_SubsystemRefCount[subsystem_index] > 0) {
  145. ++SDL_SubsystemRefCount[subsystem_index];
  146. return SDL_TRUE;
  147. }
  148. return SDL_InitSubSystem(subsystem) == 0;
  149. }
  150. void SDL_SetMainReady(void)
  151. {
  152. SDL_MainIsReady = SDL_TRUE;
  153. }
  154. int SDL_InitSubSystem(Uint32 flags)
  155. {
  156. Uint32 flags_initialized = 0;
  157. if (!SDL_MainIsReady) {
  158. return SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
  159. }
  160. SDL_InitLog();
  161. SDL_InitProperties();
  162. SDL_GetGlobalProperties();
  163. /* Clear the error message */
  164. SDL_ClearError();
  165. #ifdef SDL_USE_LIBDBUS
  166. SDL_DBus_Init();
  167. #endif
  168. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  169. if (flags & (SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK)) {
  170. if (SDL_HelperWindowCreate() < 0) {
  171. goto quit_and_error;
  172. }
  173. }
  174. #endif
  175. SDL_InitTicks();
  176. /* Initialize the event subsystem */
  177. if (flags & SDL_INIT_EVENTS) {
  178. if (SDL_ShouldInitSubsystem(SDL_INIT_EVENTS)) {
  179. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  180. if (SDL_InitEvents() < 0) {
  181. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  182. goto quit_and_error;
  183. }
  184. } else {
  185. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  186. }
  187. flags_initialized |= SDL_INIT_EVENTS;
  188. }
  189. /* Initialize the timer subsystem */
  190. if (flags & SDL_INIT_TIMER) {
  191. if (SDL_ShouldInitSubsystem(SDL_INIT_TIMER)) {
  192. SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
  193. if (SDL_InitTimers() < 0) {
  194. SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
  195. goto quit_and_error;
  196. }
  197. } else {
  198. SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
  199. }
  200. flags_initialized |= SDL_INIT_TIMER;
  201. }
  202. /* Initialize the video subsystem */
  203. if (flags & SDL_INIT_VIDEO) {
  204. #ifndef SDL_VIDEO_DISABLED
  205. if (SDL_ShouldInitSubsystem(SDL_INIT_VIDEO)) {
  206. /* video implies events */
  207. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  208. goto quit_and_error;
  209. }
  210. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  211. if (SDL_VideoInit(NULL) < 0) {
  212. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  213. goto quit_and_error;
  214. }
  215. } else {
  216. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  217. }
  218. flags_initialized |= SDL_INIT_VIDEO;
  219. #else
  220. SDL_SetError("SDL not built with video support");
  221. goto quit_and_error;
  222. #endif
  223. }
  224. /* Initialize the audio subsystem */
  225. if (flags & SDL_INIT_AUDIO) {
  226. #ifndef SDL_AUDIO_DISABLED
  227. if (SDL_ShouldInitSubsystem(SDL_INIT_AUDIO)) {
  228. /* audio implies events */
  229. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  230. goto quit_and_error;
  231. }
  232. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  233. if (SDL_InitAudio(NULL) < 0) {
  234. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  235. goto quit_and_error;
  236. }
  237. } else {
  238. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  239. }
  240. flags_initialized |= SDL_INIT_AUDIO;
  241. #else
  242. SDL_SetError("SDL not built with audio support");
  243. goto quit_and_error;
  244. #endif
  245. }
  246. /* Initialize the joystick subsystem */
  247. if (flags & SDL_INIT_JOYSTICK) {
  248. #ifndef SDL_JOYSTICK_DISABLED
  249. if (SDL_ShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
  250. /* joystick implies events */
  251. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  252. goto quit_and_error;
  253. }
  254. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  255. if (SDL_InitJoysticks() < 0) {
  256. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  257. goto quit_and_error;
  258. }
  259. } else {
  260. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  261. }
  262. flags_initialized |= SDL_INIT_JOYSTICK;
  263. #else
  264. SDL_SetError("SDL not built with joystick support");
  265. goto quit_and_error;
  266. #endif
  267. }
  268. if (flags & SDL_INIT_GAMEPAD) {
  269. #ifndef SDL_JOYSTICK_DISABLED
  270. if (SDL_ShouldInitSubsystem(SDL_INIT_GAMEPAD)) {
  271. /* game controller implies joystick */
  272. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_JOYSTICK)) {
  273. goto quit_and_error;
  274. }
  275. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  276. if (SDL_InitGamepads() < 0) {
  277. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  278. goto quit_and_error;
  279. }
  280. } else {
  281. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  282. }
  283. flags_initialized |= SDL_INIT_GAMEPAD;
  284. #else
  285. SDL_SetError("SDL not built with joystick support");
  286. goto quit_and_error;
  287. #endif
  288. }
  289. /* Initialize the haptic subsystem */
  290. if (flags & SDL_INIT_HAPTIC) {
  291. #ifndef SDL_HAPTIC_DISABLED
  292. if (SDL_ShouldInitSubsystem(SDL_INIT_HAPTIC)) {
  293. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  294. if (SDL_InitHaptics() < 0) {
  295. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  296. goto quit_and_error;
  297. }
  298. } else {
  299. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  300. }
  301. flags_initialized |= SDL_INIT_HAPTIC;
  302. #else
  303. SDL_SetError("SDL not built with haptic (force feedback) support");
  304. goto quit_and_error;
  305. #endif
  306. }
  307. /* Initialize the sensor subsystem */
  308. if (flags & SDL_INIT_SENSOR) {
  309. #ifndef SDL_SENSOR_DISABLED
  310. if (SDL_ShouldInitSubsystem(SDL_INIT_SENSOR)) {
  311. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  312. if (SDL_InitSensors() < 0) {
  313. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  314. goto quit_and_error;
  315. }
  316. } else {
  317. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  318. }
  319. flags_initialized |= SDL_INIT_SENSOR;
  320. #else
  321. SDL_SetError("SDL not built with sensor support");
  322. goto quit_and_error;
  323. #endif
  324. }
  325. (void)flags_initialized; /* make static analysis happy, since this only gets used in error cases. */
  326. return 0;
  327. quit_and_error:
  328. SDL_QuitSubSystem(flags_initialized);
  329. return -1;
  330. }
  331. int SDL_Init(Uint32 flags)
  332. {
  333. return SDL_InitSubSystem(flags);
  334. }
  335. void SDL_QuitSubSystem(Uint32 flags)
  336. {
  337. /* Shut down requested initialized subsystems */
  338. #ifndef SDL_SENSOR_DISABLED
  339. if (flags & SDL_INIT_SENSOR) {
  340. if (SDL_ShouldQuitSubsystem(SDL_INIT_SENSOR)) {
  341. SDL_QuitSensors();
  342. }
  343. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  344. }
  345. #endif
  346. #ifndef SDL_JOYSTICK_DISABLED
  347. if (flags & SDL_INIT_GAMEPAD) {
  348. if (SDL_ShouldQuitSubsystem(SDL_INIT_GAMEPAD)) {
  349. SDL_QuitGamepads();
  350. /* game controller implies joystick */
  351. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  352. }
  353. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  354. }
  355. if (flags & SDL_INIT_JOYSTICK) {
  356. if (SDL_ShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  357. SDL_QuitJoysticks();
  358. /* joystick implies events */
  359. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  360. }
  361. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  362. }
  363. #endif
  364. #ifndef SDL_HAPTIC_DISABLED
  365. if (flags & SDL_INIT_HAPTIC) {
  366. if (SDL_ShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  367. SDL_QuitHaptics();
  368. }
  369. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  370. }
  371. #endif
  372. #ifndef SDL_AUDIO_DISABLED
  373. if (flags & SDL_INIT_AUDIO) {
  374. if (SDL_ShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  375. SDL_QuitAudio();
  376. /* audio implies events */
  377. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  378. }
  379. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  380. }
  381. #endif
  382. #ifndef SDL_VIDEO_DISABLED
  383. if (flags & SDL_INIT_VIDEO) {
  384. if (SDL_ShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  385. SDL_VideoQuit();
  386. /* video implies events */
  387. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  388. }
  389. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  390. }
  391. #endif
  392. if (flags & SDL_INIT_TIMER) {
  393. if (SDL_ShouldQuitSubsystem(SDL_INIT_TIMER)) {
  394. SDL_QuitTimers();
  395. }
  396. SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
  397. }
  398. if (flags & SDL_INIT_EVENTS) {
  399. if (SDL_ShouldQuitSubsystem(SDL_INIT_EVENTS)) {
  400. SDL_QuitEvents();
  401. }
  402. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  403. }
  404. }
  405. Uint32 SDL_WasInit(Uint32 flags)
  406. {
  407. int i;
  408. int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
  409. Uint32 initialized = 0;
  410. /* Fast path for checking one flag */
  411. if (SDL_HasExactlyOneBitSet32(flags)) {
  412. int subsystem_index = SDL_MostSignificantBitIndex32(flags);
  413. return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
  414. }
  415. if (!flags) {
  416. flags = SDL_INIT_EVERYTHING;
  417. }
  418. num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
  419. /* Iterate over each bit in flags, and check the matching subsystem. */
  420. for (i = 0; i < num_subsystems; ++i) {
  421. if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
  422. initialized |= (1 << i);
  423. }
  424. flags >>= 1;
  425. }
  426. return initialized;
  427. }
  428. void SDL_Quit(void)
  429. {
  430. SDL_bInMainQuit = SDL_TRUE;
  431. /* Quit all subsystems */
  432. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  433. SDL_HelperWindowDestroy();
  434. #endif
  435. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  436. SDL_QuitTicks();
  437. #ifdef SDL_USE_LIBDBUS
  438. SDL_DBus_Quit();
  439. #endif
  440. SDL_ClearHints();
  441. SDL_AssertionsQuit();
  442. SDL_QuitProperties();
  443. SDL_QuitLog();
  444. /* Now that every subsystem has been quit, we reset the subsystem refcount
  445. * and the list of initialized subsystems.
  446. */
  447. SDL_memset(SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount));
  448. SDL_CleanupTLS();
  449. SDL_bInMainQuit = SDL_FALSE;
  450. }
  451. /* Assume we can wrap SDL_AtomicInt values and cast to Uint32 */
  452. SDL_COMPILE_TIME_ASSERT(sizeof_object_id, sizeof(int) == sizeof(Uint32));
  453. Uint32 SDL_GetNextObjectID(void)
  454. {
  455. static SDL_AtomicInt last_id;
  456. Uint32 id = (Uint32)SDL_AtomicIncRef(&last_id) + 1;
  457. if (id == 0) {
  458. id = (Uint32)SDL_AtomicIncRef(&last_id) + 1;
  459. }
  460. return id;
  461. }
  462. /* Get the library version number */
  463. int SDL_GetVersion(SDL_version *ver)
  464. {
  465. static SDL_bool check_hint = SDL_TRUE;
  466. static SDL_bool legacy_version = SDL_FALSE;
  467. if (!ver) {
  468. return SDL_InvalidParamError("ver");
  469. }
  470. SDL_VERSION(ver);
  471. if (check_hint) {
  472. check_hint = SDL_FALSE;
  473. legacy_version = SDL_GetHintBoolean("SDL_LEGACY_VERSION", SDL_FALSE);
  474. }
  475. if (legacy_version) {
  476. /* Prior to SDL 2.24.0, the patch version was incremented with every release */
  477. ver->patch = ver->minor;
  478. ver->minor = 0;
  479. }
  480. return 0;
  481. }
  482. /* Get the library source revision */
  483. const char *SDL_GetRevision(void)
  484. {
  485. return SDL_REVISION;
  486. }
  487. /* Get the name of the platform */
  488. const char *SDL_GetPlatform(void)
  489. {
  490. #if defined(SDL_PLATFORM_AIX)
  491. return "AIX";
  492. #elif defined(SDL_PLATFORM_ANDROID)
  493. return "Android";
  494. #elif defined(SDL_PLATFORM_BSDI)
  495. return "BSDI";
  496. #elif defined(SDL_PLATFORM_EMSCRIPTEN)
  497. return "Emscripten";
  498. #elif defined(SDL_PLATFORM_FREEBSD)
  499. return "FreeBSD";
  500. #elif defined(SDL_PLATFORM_HAIKU)
  501. return "Haiku";
  502. #elif defined(SDL_PLATFORM_HPUX)
  503. return "HP-UX";
  504. #elif defined(SDL_PLATFORM_IRIX)
  505. return "Irix";
  506. #elif defined(SDL_PLATFORM_LINUX)
  507. return "Linux";
  508. #elif defined(__MINT__)
  509. return "Atari MiNT";
  510. #elif defined(SDL_PLATFORM_MACOS)
  511. return "macOS";
  512. #elif defined(SDL_PLATFORM_NETBSD)
  513. return "NetBSD";
  514. #elif defined(SDL_PLATFORM_OPENBSD)
  515. return "OpenBSD";
  516. #elif defined(SDL_PLATFORM_OS2)
  517. return "OS/2";
  518. #elif defined(SDL_PLATFORM_OSF)
  519. return "OSF/1";
  520. #elif defined(SDL_PLATFORM_QNXNTO)
  521. return "QNX Neutrino";
  522. #elif defined(SDL_PLATFORM_RISCOS)
  523. return "RISC OS";
  524. #elif defined(SDL_PLATFORM_SOLARIS)
  525. return "Solaris";
  526. #elif defined(SDL_PLATFORM_WIN32)
  527. return "Windows";
  528. #elif defined(SDL_PLATFORM_WINRT)
  529. return "WinRT";
  530. #elif defined(SDL_PLATFORM_WINGDK)
  531. return "WinGDK";
  532. #elif defined(SDL_PLATFORM_XBOXONE)
  533. return "Xbox One";
  534. #elif defined(SDL_PLATFORM_XBOXSERIES)
  535. return "Xbox Series X|S";
  536. #elif defined(SDL_PLATFORM_IOS)
  537. return "iOS";
  538. #elif defined(SDL_PLATFORM_TVOS)
  539. return "tvOS";
  540. #elif defined(SDL_PLATFORM_PS2)
  541. return "PlayStation 2";
  542. #elif defined(SDL_PLATFORM_PSP)
  543. return "PlayStation Portable";
  544. #elif defined(SDL_PLATFORM_VITA)
  545. return "PlayStation Vita";
  546. #elif defined(SDL_PLATFORM_NGAGE)
  547. return "Nokia N-Gage";
  548. #elif defined(SDL_PLATFORM_3DS)
  549. return "Nintendo 3DS";
  550. #elif defined(__managarm__)
  551. return "Managarm";
  552. #else
  553. return "Unknown (see SDL_platform.h)";
  554. #endif
  555. }
  556. SDL_bool SDL_IsTablet(void)
  557. {
  558. #ifdef SDL_PLATFORM_ANDROID
  559. extern SDL_bool SDL_IsAndroidTablet(void);
  560. return SDL_IsAndroidTablet();
  561. #elif defined(SDL_PLATFORM_IOS)
  562. extern SDL_bool SDL_IsIPad(void);
  563. return SDL_IsIPad();
  564. #else
  565. return SDL_FALSE;
  566. #endif
  567. }
  568. #ifdef SDL_PLATFORM_WIN32
  569. #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
  570. /* FIXME: Still need to include DllMain() on Watcom C ? */
  571. BOOL APIENTRY MINGW32_FORCEALIGN _DllMainCRTStartup(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  572. {
  573. switch (ul_reason_for_call) {
  574. case DLL_PROCESS_ATTACH:
  575. case DLL_THREAD_ATTACH:
  576. case DLL_THREAD_DETACH:
  577. case DLL_PROCESS_DETACH:
  578. break;
  579. }
  580. return TRUE;
  581. }
  582. #endif /* Building DLL */
  583. #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */