SDL.c 12 KB

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