SDL.c 12 KB

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