SDL.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. #if defined(__WIN32__)
  20. #include "core/windows/SDL_windows.h"
  21. #elif defined(__OS2__)
  22. #include <stdlib.h> /* _exit() */
  23. #elif !defined(__WINRT__)
  24. #include <unistd.h> /* _exit(), etc. */
  25. #endif
  26. #if defined(__OS2__)
  27. #include "core/os2/SDL_os2.h"
  28. #if SDL_THREAD_OS2
  29. #include "thread/os2/SDL_systls_c.h"
  30. #endif
  31. #endif
  32. /* this checks for HAVE_DBUS_DBUS_H internally. */
  33. #include "core/linux/SDL_dbus.h"
  34. #if defined(__EMSCRIPTEN__)
  35. #include <emscripten.h>
  36. #endif
  37. /* Initialization code for SDL */
  38. #include "SDL.h"
  39. #include "SDL_bits.h"
  40. #include "SDL_revision.h"
  41. #include "SDL_assert_c.h"
  42. #include "events/SDL_events_c.h"
  43. #include "haptic/SDL_haptic_c.h"
  44. #include "joystick/SDL_joystick_c.h"
  45. #include "sensor/SDL_sensor_c.h"
  46. /* Initialization/Cleanup routines */
  47. #if !SDL_TIMERS_DISABLED
  48. # include "timer/SDL_timer_c.h"
  49. #endif
  50. #if SDL_VIDEO_DRIVER_WINDOWS
  51. extern int SDL_HelperWindowCreate(void);
  52. extern int SDL_HelperWindowDestroy(void);
  53. #endif
  54. /* This is not declared in any header, although it is shared between some
  55. parts of SDL, because we don't want anything calling it without an
  56. extremely good reason. */
  57. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  58. SDL_NORETURN void SDL_ExitProcess(int exitcode)
  59. {
  60. #ifdef __WIN32__
  61. /* "if you do not know the state of all threads in your process, it is
  62. better to call TerminateProcess than ExitProcess"
  63. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx */
  64. TerminateProcess(GetCurrentProcess(), exitcode);
  65. /* MingW doesn't have TerminateProcess marked as noreturn, so add an
  66. ExitProcess here that will never be reached but make MingW happy. */
  67. ExitProcess(exitcode);
  68. #elif defined(__EMSCRIPTEN__)
  69. emscripten_cancel_main_loop(); /* this should "kill" the app. */
  70. emscripten_force_exit(exitcode); /* this should "kill" the app. */
  71. exit(exitcode);
  72. #elif defined(__HAIKU__) /* Haiku has _Exit, but it's not marked noreturn. */
  73. _exit(exitcode);
  74. #elif defined(HAVE__EXIT) /* Upper case _Exit() */
  75. _Exit(exitcode);
  76. #else
  77. _exit(exitcode);
  78. #endif
  79. }
  80. /* The initialized subsystems */
  81. #ifdef SDL_MAIN_NEEDED
  82. static SDL_bool SDL_MainIsReady = SDL_FALSE;
  83. #else
  84. static SDL_bool SDL_MainIsReady = SDL_TRUE;
  85. #endif
  86. static SDL_bool SDL_bInMainQuit = SDL_FALSE;
  87. static Uint8 SDL_SubsystemRefCount[ 32 ];
  88. /* Private helper to increment a subsystem's ref counter. */
  89. static void
  90. SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem)
  91. {
  92. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  93. SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
  94. ++SDL_SubsystemRefCount[subsystem_index];
  95. }
  96. /* Private helper to decrement a subsystem's ref counter. */
  97. static void
  98. SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem)
  99. {
  100. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  101. if (SDL_SubsystemRefCount[subsystem_index] > 0) {
  102. --SDL_SubsystemRefCount[subsystem_index];
  103. }
  104. }
  105. /* Private helper to check if a system needs init. */
  106. static SDL_bool
  107. SDL_PrivateShouldInitSubsystem(Uint32 subsystem)
  108. {
  109. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  110. SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
  111. return (SDL_SubsystemRefCount[subsystem_index] == 0) ? SDL_TRUE : SDL_FALSE;
  112. }
  113. /* Private helper to check if a system needs to be quit. */
  114. static SDL_bool
  115. SDL_PrivateShouldQuitSubsystem(Uint32 subsystem) {
  116. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  117. if (SDL_SubsystemRefCount[subsystem_index] == 0) {
  118. return SDL_FALSE;
  119. }
  120. /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
  121. * isn't zero.
  122. */
  123. return (SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit) ? SDL_TRUE : SDL_FALSE;
  124. }
  125. void
  126. SDL_SetMainReady(void)
  127. {
  128. SDL_MainIsReady = SDL_TRUE;
  129. }
  130. void SDL_LogInit(void);
  131. void SDL_LogQuit(void);
  132. int
  133. SDL_InitSubSystem(Uint32 flags)
  134. {
  135. Uint32 flags_initialized = 0;
  136. if (!SDL_MainIsReady) {
  137. return SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
  138. }
  139. SDL_LogInit();
  140. /* Clear the error message */
  141. SDL_ClearError();
  142. #if SDL_USE_LIBDBUS
  143. SDL_DBus_Init();
  144. #endif
  145. if ((flags & SDL_INIT_GAMECONTROLLER)) {
  146. /* game controller implies joystick */
  147. flags |= SDL_INIT_JOYSTICK;
  148. }
  149. if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK|SDL_INIT_AUDIO))) {
  150. /* video or joystick or audio implies events */
  151. flags |= SDL_INIT_EVENTS;
  152. }
  153. #if SDL_THREAD_OS2
  154. SDL_OS2TLSAlloc(); /* thread/os2/SDL_systls.c */
  155. #endif
  156. #if SDL_VIDEO_DRIVER_WINDOWS
  157. if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {
  158. if (SDL_HelperWindowCreate() < 0) {
  159. goto quit_and_error;
  160. }
  161. }
  162. #endif
  163. #if !SDL_TIMERS_DISABLED
  164. SDL_TicksInit();
  165. #endif
  166. /* Initialize the event subsystem */
  167. if ((flags & SDL_INIT_EVENTS)) {
  168. #if !SDL_EVENTS_DISABLED
  169. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {
  170. if (SDL_EventsInit() < 0) {
  171. goto quit_and_error;
  172. }
  173. }
  174. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);
  175. flags_initialized |= SDL_INIT_EVENTS;
  176. #else
  177. SDL_SetError("SDL not built with events support");
  178. goto quit_and_error;
  179. #endif
  180. }
  181. /* Initialize the timer subsystem */
  182. if ((flags & SDL_INIT_TIMER)){
  183. #if !SDL_TIMERS_DISABLED && !SDL_TIMER_DUMMY
  184. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {
  185. if (SDL_TimerInit() < 0) {
  186. goto quit_and_error;
  187. }
  188. }
  189. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
  190. flags_initialized |= SDL_INIT_TIMER;
  191. #else
  192. SDL_SetError("SDL not built with timer support");
  193. goto quit_and_error;
  194. #endif
  195. }
  196. /* Initialize the video subsystem */
  197. if ((flags & SDL_INIT_VIDEO)){
  198. #if !SDL_VIDEO_DISABLED
  199. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {
  200. if (SDL_VideoInit(NULL) < 0) {
  201. goto quit_and_error;
  202. }
  203. }
  204. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
  205. flags_initialized |= SDL_INIT_VIDEO;
  206. #else
  207. SDL_SetError("SDL not built with video support");
  208. goto quit_and_error;
  209. #endif
  210. }
  211. /* Initialize the audio subsystem */
  212. if ((flags & SDL_INIT_AUDIO)){
  213. #if !SDL_AUDIO_DISABLED
  214. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) {
  215. if (SDL_AudioInit(NULL) < 0) {
  216. goto quit_and_error;
  217. }
  218. }
  219. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
  220. flags_initialized |= SDL_INIT_AUDIO;
  221. #else
  222. SDL_SetError("SDL not built with audio support");
  223. goto quit_and_error;
  224. #endif
  225. }
  226. /* Initialize the joystick subsystem */
  227. if ((flags & SDL_INIT_JOYSTICK)){
  228. #if !SDL_JOYSTICK_DISABLED
  229. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
  230. if (SDL_JoystickInit() < 0) {
  231. goto quit_and_error;
  232. }
  233. }
  234. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
  235. flags_initialized |= SDL_INIT_JOYSTICK;
  236. #else
  237. SDL_SetError("SDL not built with joystick support");
  238. goto quit_and_error;
  239. #endif
  240. }
  241. if ((flags & SDL_INIT_GAMECONTROLLER)){
  242. #if !SDL_JOYSTICK_DISABLED
  243. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {
  244. if (SDL_GameControllerInit() < 0) {
  245. goto quit_and_error;
  246. }
  247. }
  248. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
  249. flags_initialized |= SDL_INIT_GAMECONTROLLER;
  250. #else
  251. SDL_SetError("SDL not built with joystick support");
  252. goto quit_and_error;
  253. #endif
  254. }
  255. /* Initialize the haptic subsystem */
  256. if ((flags & SDL_INIT_HAPTIC)){
  257. #if !SDL_HAPTIC_DISABLED
  258. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {
  259. if (SDL_HapticInit() < 0) {
  260. goto quit_and_error;
  261. }
  262. }
  263. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
  264. flags_initialized |= SDL_INIT_HAPTIC;
  265. #else
  266. SDL_SetError("SDL not built with haptic (force feedback) support");
  267. goto quit_and_error;
  268. #endif
  269. }
  270. /* Initialize the sensor subsystem */
  271. if ((flags & SDL_INIT_SENSOR)){
  272. #if !SDL_SENSOR_DISABLED
  273. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_SENSOR)) {
  274. if (SDL_SensorInit() < 0) {
  275. goto quit_and_error;
  276. }
  277. }
  278. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_SENSOR);
  279. flags_initialized |= SDL_INIT_SENSOR;
  280. #else
  281. SDL_SetError("SDL not built with sensor support");
  282. goto quit_and_error;
  283. #endif
  284. }
  285. (void) flags_initialized; /* make static analysis happy, since this only gets used in error cases. */
  286. return (0);
  287. quit_and_error:
  288. SDL_QuitSubSystem(flags_initialized);
  289. return (-1);
  290. }
  291. int
  292. SDL_Init(Uint32 flags)
  293. {
  294. return SDL_InitSubSystem(flags);
  295. }
  296. void
  297. SDL_QuitSubSystem(Uint32 flags)
  298. {
  299. #if defined(__OS2__)
  300. #if SDL_THREAD_OS2
  301. SDL_OS2TLSFree(); /* thread/os2/SDL_systls.c */
  302. #endif
  303. SDL_OS2Quit();
  304. #endif
  305. /* Shut down requested initialized subsystems */
  306. #if !SDL_SENSOR_DISABLED
  307. if ((flags & SDL_INIT_SENSOR)) {
  308. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_SENSOR)) {
  309. SDL_SensorQuit();
  310. }
  311. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_SENSOR);
  312. }
  313. #endif
  314. #if !SDL_JOYSTICK_DISABLED
  315. if ((flags & SDL_INIT_GAMECONTROLLER)) {
  316. /* game controller implies joystick */
  317. flags |= SDL_INIT_JOYSTICK;
  318. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) {
  319. SDL_GameControllerQuit();
  320. }
  321. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_GAMECONTROLLER);
  322. }
  323. if ((flags & SDL_INIT_JOYSTICK)) {
  324. /* joystick implies events */
  325. flags |= SDL_INIT_EVENTS;
  326. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  327. SDL_JoystickQuit();
  328. }
  329. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_JOYSTICK);
  330. }
  331. #endif
  332. #if !SDL_HAPTIC_DISABLED
  333. if ((flags & SDL_INIT_HAPTIC)) {
  334. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  335. SDL_HapticQuit();
  336. }
  337. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_HAPTIC);
  338. }
  339. #endif
  340. #if !SDL_AUDIO_DISABLED
  341. if ((flags & SDL_INIT_AUDIO)) {
  342. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  343. SDL_AudioQuit();
  344. }
  345. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_AUDIO);
  346. }
  347. #endif
  348. #if !SDL_VIDEO_DISABLED
  349. if ((flags & SDL_INIT_VIDEO)) {
  350. /* video implies events */
  351. flags |= SDL_INIT_EVENTS;
  352. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  353. SDL_VideoQuit();
  354. }
  355. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_VIDEO);
  356. }
  357. #endif
  358. #if !SDL_TIMERS_DISABLED && !SDL_TIMER_DUMMY
  359. if ((flags & SDL_INIT_TIMER)) {
  360. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_TIMER)) {
  361. SDL_TimerQuit();
  362. }
  363. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER);
  364. }
  365. #endif
  366. #if !SDL_EVENTS_DISABLED
  367. if ((flags & SDL_INIT_EVENTS)) {
  368. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_EVENTS)) {
  369. SDL_EventsQuit();
  370. }
  371. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_EVENTS);
  372. }
  373. #endif
  374. }
  375. Uint32
  376. SDL_WasInit(Uint32 flags)
  377. {
  378. int i;
  379. int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
  380. Uint32 initialized = 0;
  381. /* Fast path for checking one flag */
  382. if (SDL_HasExactlyOneBitSet32(flags)) {
  383. int subsystem_index = SDL_MostSignificantBitIndex32(flags);
  384. return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
  385. }
  386. if (!flags) {
  387. flags = SDL_INIT_EVERYTHING;
  388. }
  389. num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
  390. /* Iterate over each bit in flags, and check the matching subsystem. */
  391. for (i = 0; i < num_subsystems; ++i) {
  392. if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
  393. initialized |= (1 << i);
  394. }
  395. flags >>= 1;
  396. }
  397. return initialized;
  398. }
  399. void
  400. SDL_Quit(void)
  401. {
  402. SDL_bInMainQuit = SDL_TRUE;
  403. /* Quit all subsystems */
  404. #if SDL_VIDEO_DRIVER_WINDOWS
  405. SDL_HelperWindowDestroy();
  406. #endif
  407. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  408. #if !SDL_TIMERS_DISABLED
  409. SDL_TicksQuit();
  410. #endif
  411. SDL_ClearHints();
  412. SDL_AssertionsQuit();
  413. #if SDL_USE_LIBDBUS
  414. SDL_DBus_Quit();
  415. #endif
  416. SDL_LogQuit();
  417. /* Now that every subsystem has been quit, we reset the subsystem refcount
  418. * and the list of initialized subsystems.
  419. */
  420. SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
  421. SDL_bInMainQuit = SDL_FALSE;
  422. }
  423. /* Get the library version number */
  424. void
  425. SDL_GetVersion(SDL_version * ver)
  426. {
  427. SDL_VERSION(ver);
  428. }
  429. /* Get the library source revision */
  430. const char *
  431. SDL_GetRevision(void)
  432. {
  433. return SDL_REVISION;
  434. }
  435. /* Get the library source revision number */
  436. int
  437. SDL_GetRevisionNumber(void)
  438. {
  439. return 0; /* doesn't make sense without Mercurial. */
  440. }
  441. /* Get the name of the platform */
  442. const char *
  443. SDL_GetPlatform(void)
  444. {
  445. #if __AIX__
  446. return "AIX";
  447. #elif __ANDROID__
  448. return "Android";
  449. #elif __BSDI__
  450. return "BSDI";
  451. #elif __DREAMCAST__
  452. return "Dreamcast";
  453. #elif __EMSCRIPTEN__
  454. return "Emscripten";
  455. #elif __FREEBSD__
  456. return "FreeBSD";
  457. #elif __HAIKU__
  458. return "Haiku";
  459. #elif __HPUX__
  460. return "HP-UX";
  461. #elif __IRIX__
  462. return "Irix";
  463. #elif __LINUX__
  464. return "Linux";
  465. #elif __MINT__
  466. return "Atari MiNT";
  467. #elif __MACOS__
  468. return "MacOS Classic";
  469. #elif __MACOSX__
  470. return "Mac OS X";
  471. #elif __NACL__
  472. return "NaCl";
  473. #elif __NETBSD__
  474. return "NetBSD";
  475. #elif __OPENBSD__
  476. return "OpenBSD";
  477. #elif __OS2__
  478. return "OS/2";
  479. #elif __OSF__
  480. return "OSF/1";
  481. #elif __QNXNTO__
  482. return "QNX Neutrino";
  483. #elif __RISCOS__
  484. return "RISC OS";
  485. #elif __SOLARIS__
  486. return "Solaris";
  487. #elif __WIN32__
  488. return "Windows";
  489. #elif __WINRT__
  490. return "WinRT";
  491. #elif __TVOS__
  492. return "tvOS";
  493. #elif __IPHONEOS__
  494. return "iOS";
  495. #elif __PSP__
  496. return "PlayStation Portable";
  497. #elif __VITA__
  498. return "PlayStation Vita";
  499. #else
  500. return "Unknown (see SDL_platform.h)";
  501. #endif
  502. }
  503. SDL_bool
  504. SDL_IsTablet(void)
  505. {
  506. #if __ANDROID__
  507. extern SDL_bool SDL_IsAndroidTablet(void);
  508. return SDL_IsAndroidTablet();
  509. #elif __IPHONEOS__
  510. extern SDL_bool SDL_IsIPad(void);
  511. return SDL_IsIPad();
  512. #else
  513. return SDL_FALSE;
  514. #endif
  515. }
  516. #if defined(__WIN32__)
  517. #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
  518. /* Need to include DllMain() on Watcom C for some reason.. */
  519. BOOL APIENTRY
  520. _DllMainCRTStartup(HANDLE hModule,
  521. DWORD ul_reason_for_call, LPVOID lpReserved)
  522. {
  523. switch (ul_reason_for_call) {
  524. case DLL_PROCESS_ATTACH:
  525. case DLL_THREAD_ATTACH:
  526. case DLL_THREAD_DETACH:
  527. case DLL_PROCESS_DETACH:
  528. break;
  529. }
  530. return TRUE;
  531. }
  532. #endif /* Building DLL */
  533. #endif /* __WIN32__ */
  534. /* vi: set sts=4 ts=4 sw=4 expandtab: */