SDL.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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_StartEventLoop() < 0) {
  121. return (-1);
  122. }
  123. SDL_QuitInit();
  124. }
  125. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);
  126. #else
  127. return SDL_SetError("SDL not built with events support");
  128. #endif
  129. }
  130. /* Initialize the timer subsystem */
  131. if ((flags & SDL_INIT_TIMER)){
  132. #if !SDL_TIMERS_DISABLED
  133. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {
  134. if (SDL_TimerInit() < 0) {
  135. return (-1);
  136. }
  137. }
  138. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
  139. #else
  140. return SDL_SetError("SDL not built with timer support");
  141. #endif
  142. }
  143. /* Initialize the video subsystem */
  144. if ((flags & SDL_INIT_VIDEO)){
  145. #if !SDL_VIDEO_DISABLED
  146. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {
  147. if (SDL_VideoInit(NULL) < 0) {
  148. return (-1);
  149. }
  150. }
  151. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
  152. #else
  153. return SDL_SetError("SDL not built with video support");
  154. #endif
  155. }
  156. /* Initialize the audio subsystem */
  157. if ((flags & SDL_INIT_AUDIO)){
  158. #if !SDL_AUDIO_DISABLED
  159. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) {
  160. if (SDL_AudioInit(NULL) < 0) {
  161. return (-1);
  162. }
  163. }
  164. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
  165. #else
  166. return SDL_SetError("SDL not built with audio support");
  167. #endif
  168. }
  169. /* Initialize the joystick subsystem */
  170. if ((flags & SDL_INIT_JOYSTICK)){
  171. #if !SDL_JOYSTICK_DISABLED
  172. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
  173. if (SDL_JoystickInit() < 0) {
  174. return (-1);
  175. }
  176. }
  177. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
  178. #else
  179. return SDL_SetError("SDL not built with joystick support");
  180. #endif
  181. }
  182. if ((flags & SDL_INIT_GAMECONTROLLER)){
  183. #if !SDL_JOYSTICK_DISABLED
  184. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {
  185. if (SDL_GameControllerInit() < 0) {
  186. return (-1);
  187. }
  188. }
  189. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
  190. #else
  191. return SDL_SetError("SDL not built with joystick support");
  192. #endif
  193. }
  194. /* Initialize the haptic subsystem */
  195. if ((flags & SDL_INIT_HAPTIC)){
  196. #if !SDL_HAPTIC_DISABLED
  197. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {
  198. if (SDL_HapticInit() < 0) {
  199. return (-1);
  200. }
  201. }
  202. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
  203. #else
  204. return SDL_SetError("SDL not built with haptic (force feedback) support");
  205. #endif
  206. }
  207. /* Initialize the sensor subsystem */
  208. if ((flags & SDL_INIT_SENSOR)){
  209. #if !SDL_SENSOR_DISABLED
  210. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_SENSOR)) {
  211. if (SDL_SensorInit() < 0) {
  212. return (-1);
  213. }
  214. }
  215. SDL_PrivateSubsystemRefCountIncr(SDL_INIT_SENSOR);
  216. #else
  217. return SDL_SetError("SDL not built with sensor support");
  218. #endif
  219. }
  220. return (0);
  221. }
  222. int
  223. SDL_Init(Uint32 flags)
  224. {
  225. return SDL_InitSubSystem(flags);
  226. }
  227. void
  228. SDL_QuitSubSystem(Uint32 flags)
  229. {
  230. /* Shut down requested initialized subsystems */
  231. #if !SDL_SENSOR_DISABLED
  232. if ((flags & SDL_INIT_SENSOR)) {
  233. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_SENSOR)) {
  234. SDL_SensorQuit();
  235. }
  236. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_SENSOR);
  237. }
  238. #endif
  239. #if !SDL_JOYSTICK_DISABLED
  240. if ((flags & SDL_INIT_GAMECONTROLLER)) {
  241. /* game controller implies joystick */
  242. flags |= SDL_INIT_JOYSTICK;
  243. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) {
  244. SDL_GameControllerQuit();
  245. }
  246. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_GAMECONTROLLER);
  247. }
  248. if ((flags & SDL_INIT_JOYSTICK)) {
  249. /* joystick implies events */
  250. flags |= SDL_INIT_EVENTS;
  251. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  252. SDL_JoystickQuit();
  253. }
  254. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_JOYSTICK);
  255. }
  256. #endif
  257. #if !SDL_HAPTIC_DISABLED
  258. if ((flags & SDL_INIT_HAPTIC)) {
  259. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  260. SDL_HapticQuit();
  261. }
  262. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_HAPTIC);
  263. }
  264. #endif
  265. #if !SDL_AUDIO_DISABLED
  266. if ((flags & SDL_INIT_AUDIO)) {
  267. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  268. SDL_AudioQuit();
  269. }
  270. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_AUDIO);
  271. }
  272. #endif
  273. #if !SDL_VIDEO_DISABLED
  274. if ((flags & SDL_INIT_VIDEO)) {
  275. /* video implies events */
  276. flags |= SDL_INIT_EVENTS;
  277. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  278. SDL_VideoQuit();
  279. }
  280. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_VIDEO);
  281. }
  282. #endif
  283. #if !SDL_TIMERS_DISABLED
  284. if ((flags & SDL_INIT_TIMER)) {
  285. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_TIMER)) {
  286. SDL_TimerQuit();
  287. }
  288. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER);
  289. }
  290. #endif
  291. #if !SDL_EVENTS_DISABLED
  292. if ((flags & SDL_INIT_EVENTS)) {
  293. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_EVENTS)) {
  294. SDL_QuitQuit();
  295. SDL_StopEventLoop();
  296. }
  297. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_EVENTS);
  298. }
  299. #endif
  300. }
  301. Uint32
  302. SDL_WasInit(Uint32 flags)
  303. {
  304. int i;
  305. int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
  306. Uint32 initialized = 0;
  307. if (!flags) {
  308. flags = SDL_INIT_EVERYTHING;
  309. }
  310. num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
  311. /* Iterate over each bit in flags, and check the matching subsystem. */
  312. for (i = 0; i < num_subsystems; ++i) {
  313. if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
  314. initialized |= (1 << i);
  315. }
  316. flags >>= 1;
  317. }
  318. return initialized;
  319. }
  320. void
  321. SDL_Quit(void)
  322. {
  323. SDL_bInMainQuit = SDL_TRUE;
  324. /* Quit all subsystems */
  325. #if SDL_VIDEO_DRIVER_WINDOWS
  326. SDL_HelperWindowDestroy();
  327. #endif
  328. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  329. #if !SDL_TIMERS_DISABLED
  330. SDL_TicksQuit();
  331. #endif
  332. SDL_ClearHints();
  333. SDL_AssertionsQuit();
  334. SDL_LogResetPriorities();
  335. /* Now that every subsystem has been quit, we reset the subsystem refcount
  336. * and the list of initialized subsystems.
  337. */
  338. SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
  339. SDL_bInMainQuit = SDL_FALSE;
  340. }
  341. /* Get the library version number */
  342. void
  343. SDL_GetVersion(SDL_version * ver)
  344. {
  345. SDL_VERSION(ver);
  346. }
  347. /* Get the library source revision */
  348. const char *
  349. SDL_GetRevision(void)
  350. {
  351. return SDL_REVISION;
  352. }
  353. /* Get the library source revision number */
  354. int
  355. SDL_GetRevisionNumber(void)
  356. {
  357. return SDL_REVISION_NUMBER;
  358. }
  359. /* Get the name of the platform */
  360. const char *
  361. SDL_GetPlatform()
  362. {
  363. #if __AIX__
  364. return "AIX";
  365. #elif __ANDROID__
  366. return "Android";
  367. #elif __BSDI__
  368. return "BSDI";
  369. #elif __DREAMCAST__
  370. return "Dreamcast";
  371. #elif __EMSCRIPTEN__
  372. return "Emscripten";
  373. #elif __FREEBSD__
  374. return "FreeBSD";
  375. #elif __HAIKU__
  376. return "Haiku";
  377. #elif __HPUX__
  378. return "HP-UX";
  379. #elif __IRIX__
  380. return "Irix";
  381. #elif __LINUX__
  382. return "Linux";
  383. #elif __MINT__
  384. return "Atari MiNT";
  385. #elif __MACOS__
  386. return "MacOS Classic";
  387. #elif __MACOSX__
  388. return "Mac OS X";
  389. #elif __NACL__
  390. return "NaCl";
  391. #elif __NETBSD__
  392. return "NetBSD";
  393. #elif __OPENBSD__
  394. return "OpenBSD";
  395. #elif __OS2__
  396. return "OS/2";
  397. #elif __OSF__
  398. return "OSF/1";
  399. #elif __QNXNTO__
  400. return "QNX Neutrino";
  401. #elif __RISCOS__
  402. return "RISC OS";
  403. #elif __SOLARIS__
  404. return "Solaris";
  405. #elif __WIN32__
  406. return "Windows";
  407. #elif __WINRT__
  408. return "WinRT";
  409. #elif __TVOS__
  410. return "tvOS";
  411. #elif __IPHONEOS__
  412. return "iOS";
  413. #elif __PSP__
  414. return "PlayStation Portable";
  415. #else
  416. return "Unknown (see SDL_platform.h)";
  417. #endif
  418. }
  419. SDL_bool
  420. SDL_IsTablet()
  421. {
  422. #if __ANDROID__
  423. extern SDL_bool SDL_IsAndroidTablet(void);
  424. return SDL_IsAndroidTablet();
  425. #elif __IPHONEOS__
  426. extern SDL_bool SDL_IsIPad(void);
  427. return SDL_IsIPad();
  428. #else
  429. return SDL_FALSE;
  430. #endif
  431. }
  432. #if defined(__WIN32__)
  433. #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
  434. /* Need to include DllMain() on Watcom C for some reason.. */
  435. BOOL APIENTRY
  436. _DllMainCRTStartup(HANDLE hModule,
  437. DWORD ul_reason_for_call, LPVOID lpReserved)
  438. {
  439. switch (ul_reason_for_call) {
  440. case DLL_PROCESS_ATTACH:
  441. case DLL_THREAD_ATTACH:
  442. case DLL_THREAD_DETACH:
  443. case DLL_PROCESS_DETACH:
  444. break;
  445. }
  446. return TRUE;
  447. }
  448. #endif /* Building DLL */
  449. #endif /* __WIN32__ */
  450. /* vi: set sts=4 ts=4 sw=4 expandtab: */