SDL.c 12 KB

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