SDL.c 14 KB

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