SDL.c 13 KB

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