SDL.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 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. /* Initialization/Cleanup routines */
  31. #if !SDL_TIMERS_DISABLED
  32. # include "timer/SDL_timer_c.h"
  33. #endif
  34. #if SDL_VIDEO_DRIVER_WINDOWS
  35. extern int SDL_HelperWindowCreate(void);
  36. extern int SDL_HelperWindowDestroy(void);
  37. #endif
  38. /* The initialized subsystems */
  39. #ifdef SDL_MAIN_NEEDED
  40. static SDL_bool SDL_MainIsReady = SDL_FALSE;
  41. #else
  42. static SDL_bool SDL_MainIsReady = SDL_TRUE;
  43. #endif
  44. static SDL_bool SDL_bInMainQuit = SDL_FALSE;
  45. static Uint8 SDL_SubsystemRefCount[ 32 ];
  46. /* Private helper to increment a subsystem's ref counter. */
  47. static void
  48. SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem)
  49. {
  50. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  51. SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
  52. ++SDL_SubsystemRefCount[subsystem_index];
  53. }
  54. /* Private helper to decrement a subsystem's ref counter. */
  55. static void
  56. SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem)
  57. {
  58. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  59. if (SDL_SubsystemRefCount[subsystem_index] > 0) {
  60. --SDL_SubsystemRefCount[subsystem_index];
  61. }
  62. }
  63. /* Private helper to check if a system needs init. */
  64. static SDL_bool
  65. SDL_PrivateShouldInitSubsystem(Uint32 subsystem)
  66. {
  67. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  68. SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
  69. return (SDL_SubsystemRefCount[subsystem_index] == 0) ? SDL_TRUE : SDL_FALSE;
  70. }
  71. /* Private helper to check if a system needs to be quit. */
  72. static SDL_bool
  73. SDL_PrivateShouldQuitSubsystem(Uint32 subsystem) {
  74. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  75. if (SDL_SubsystemRefCount[subsystem_index] == 0) {
  76. return SDL_FALSE;
  77. }
  78. /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
  79. * isn't zero.
  80. */
  81. return (SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit) ? SDL_TRUE : SDL_FALSE;
  82. }
  83. void
  84. SDL_SetMainReady(void)
  85. {
  86. SDL_MainIsReady = SDL_TRUE;
  87. }
  88. int
  89. SDL_InitSubSystem(Uint32 flags)
  90. {
  91. if (!SDL_MainIsReady) {
  92. SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
  93. return -1;
  94. }
  95. /* Clear the error message */
  96. SDL_ClearError();
  97. if ((flags & SDL_INIT_GAMECONTROLLER)) {
  98. /* game controller implies joystick */
  99. flags |= SDL_INIT_JOYSTICK;
  100. }
  101. if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK))) {
  102. /* video or joystick implies events */
  103. flags |= SDL_INIT_EVENTS;
  104. }
  105. #if SDL_VIDEO_DRIVER_WINDOWS
  106. if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {
  107. if (SDL_HelperWindowCreate() < 0) {
  108. return -1;
  109. }
  110. }
  111. #endif
  112. #if !SDL_TIMERS_DISABLED
  113. SDL_TicksInit();
  114. #endif
  115. /* Initialize the event subsystem */
  116. if ((flags & SDL_INIT_EVENTS)) {
  117. #if !SDL_EVENTS_DISABLED
  118. if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {
  119. if (SDL_StartEventLoop() < 0) {
  120. return (-1);
  121. }
  122. SDL_QuitInit();
  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. return (0);
  207. }
  208. int
  209. SDL_Init(Uint32 flags)
  210. {
  211. return SDL_InitSubSystem(flags);
  212. }
  213. void
  214. SDL_QuitSubSystem(Uint32 flags)
  215. {
  216. /* Shut down requested initialized subsystems */
  217. #if !SDL_JOYSTICK_DISABLED
  218. if ((flags & SDL_INIT_GAMECONTROLLER)) {
  219. /* game controller implies joystick */
  220. flags |= SDL_INIT_JOYSTICK;
  221. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) {
  222. SDL_GameControllerQuit();
  223. }
  224. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_GAMECONTROLLER);
  225. }
  226. if ((flags & SDL_INIT_JOYSTICK)) {
  227. /* joystick implies events */
  228. flags |= SDL_INIT_EVENTS;
  229. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  230. SDL_JoystickQuit();
  231. }
  232. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_JOYSTICK);
  233. }
  234. #endif
  235. #if !SDL_HAPTIC_DISABLED
  236. if ((flags & SDL_INIT_HAPTIC)) {
  237. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  238. SDL_HapticQuit();
  239. }
  240. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_HAPTIC);
  241. }
  242. #endif
  243. #if !SDL_AUDIO_DISABLED
  244. if ((flags & SDL_INIT_AUDIO)) {
  245. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  246. SDL_AudioQuit();
  247. }
  248. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_AUDIO);
  249. }
  250. #endif
  251. #if !SDL_VIDEO_DISABLED
  252. if ((flags & SDL_INIT_VIDEO)) {
  253. /* video implies events */
  254. flags |= SDL_INIT_EVENTS;
  255. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  256. SDL_VideoQuit();
  257. }
  258. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_VIDEO);
  259. }
  260. #endif
  261. #if !SDL_TIMERS_DISABLED
  262. if ((flags & SDL_INIT_TIMER)) {
  263. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_TIMER)) {
  264. SDL_TimerQuit();
  265. }
  266. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER);
  267. }
  268. #endif
  269. #if !SDL_EVENTS_DISABLED
  270. if ((flags & SDL_INIT_EVENTS)) {
  271. if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_EVENTS)) {
  272. SDL_QuitQuit();
  273. SDL_StopEventLoop();
  274. }
  275. SDL_PrivateSubsystemRefCountDecr(SDL_INIT_EVENTS);
  276. }
  277. #endif
  278. }
  279. Uint32
  280. SDL_WasInit(Uint32 flags)
  281. {
  282. int i;
  283. int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
  284. Uint32 initialized = 0;
  285. if (!flags) {
  286. flags = SDL_INIT_EVERYTHING;
  287. }
  288. num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
  289. /* Iterate over each bit in flags, and check the matching subsystem. */
  290. for (i = 0; i < num_subsystems; ++i) {
  291. if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
  292. initialized |= (1 << i);
  293. }
  294. flags >>= 1;
  295. }
  296. return initialized;
  297. }
  298. void
  299. SDL_Quit(void)
  300. {
  301. SDL_bInMainQuit = SDL_TRUE;
  302. /* Quit all subsystems */
  303. #if SDL_VIDEO_DRIVER_WINDOWS
  304. SDL_HelperWindowDestroy();
  305. #endif
  306. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  307. #if !SDL_TIMERS_DISABLED
  308. SDL_TicksQuit();
  309. #endif
  310. SDL_ClearHints();
  311. SDL_AssertionsQuit();
  312. SDL_LogResetPriorities();
  313. /* Now that every subsystem has been quit, we reset the subsystem refcount
  314. * and the list of initialized subsystems.
  315. */
  316. SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
  317. SDL_bInMainQuit = SDL_FALSE;
  318. }
  319. /* Get the library version number */
  320. void
  321. SDL_GetVersion(SDL_version * ver)
  322. {
  323. SDL_VERSION(ver);
  324. }
  325. /* Get the library source revision */
  326. const char *
  327. SDL_GetRevision(void)
  328. {
  329. return SDL_REVISION;
  330. }
  331. /* Get the library source revision number */
  332. int
  333. SDL_GetRevisionNumber(void)
  334. {
  335. return SDL_REVISION_NUMBER;
  336. }
  337. /* Get the name of the platform */
  338. const char *
  339. SDL_GetPlatform()
  340. {
  341. #if __AIX__
  342. return "AIX";
  343. #elif __ANDROID__
  344. return "Android";
  345. #elif __BSDI__
  346. return "BSDI";
  347. #elif __DREAMCAST__
  348. return "Dreamcast";
  349. #elif __EMSCRIPTEN__
  350. return "Emscripten";
  351. #elif __FREEBSD__
  352. return "FreeBSD";
  353. #elif __HAIKU__
  354. return "Haiku";
  355. #elif __HPUX__
  356. return "HP-UX";
  357. #elif __IRIX__
  358. return "Irix";
  359. #elif __LINUX__
  360. return "Linux";
  361. #elif __MINT__
  362. return "Atari MiNT";
  363. #elif __MACOS__
  364. return "MacOS Classic";
  365. #elif __MACOSX__
  366. return "Mac OS X";
  367. #elif __NACL__
  368. return "NaCl";
  369. #elif __NETBSD__
  370. return "NetBSD";
  371. #elif __OPENBSD__
  372. return "OpenBSD";
  373. #elif __OS2__
  374. return "OS/2";
  375. #elif __OSF__
  376. return "OSF/1";
  377. #elif __QNXNTO__
  378. return "QNX Neutrino";
  379. #elif __RISCOS__
  380. return "RISC OS";
  381. #elif __SOLARIS__
  382. return "Solaris";
  383. #elif __WIN32__
  384. return "Windows";
  385. #elif __WINRT__
  386. return "WinRT";
  387. #elif __TVOS__
  388. return "tvOS";
  389. #elif __IPHONEOS__
  390. return "iOS";
  391. #elif __PSP__
  392. return "PlayStation Portable";
  393. #else
  394. return "Unknown (see SDL_platform.h)";
  395. #endif
  396. }
  397. #if defined(__WIN32__)
  398. #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
  399. /* Need to include DllMain() on Watcom C for some reason.. */
  400. BOOL APIENTRY
  401. _DllMainCRTStartup(HANDLE hModule,
  402. DWORD ul_reason_for_call, LPVOID lpReserved)
  403. {
  404. switch (ul_reason_for_call) {
  405. case DLL_PROCESS_ATTACH:
  406. case DLL_THREAD_ATTACH:
  407. case DLL_THREAD_DETACH:
  408. case DLL_PROCESS_DETACH:
  409. break;
  410. }
  411. return TRUE;
  412. }
  413. #endif /* building DLL with Watcom C */
  414. #endif /* __WIN32__ */
  415. /* vi: set sts=4 ts=4 sw=4 expandtab: */