SDL.c 14 KB

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