SDL.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #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 (SDL_HelperWindowCreate() < 0) {
  101. return -1;
  102. }
  103. #endif
  104. #if !SDL_TIMERS_DISABLED
  105. SDL_InitTicks();
  106. #endif
  107. if ((flags & SDL_INIT_GAMECONTROLLER)) {
  108. /* game controller implies joystick */
  109. flags |= SDL_INIT_JOYSTICK;
  110. }
  111. if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK))) {
  112. /* video or joystick implies events */
  113. flags |= SDL_INIT_EVENTS;
  114. }
  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. SDL_ClearHints();
  308. SDL_AssertionsQuit();
  309. SDL_LogResetPriorities();
  310. /* Now that every subsystem has been quit, we reset the subsystem refcount
  311. * and the list of initialized subsystems.
  312. */
  313. SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
  314. SDL_bInMainQuit = SDL_FALSE;
  315. }
  316. /* Get the library version number */
  317. void
  318. SDL_GetVersion(SDL_version * ver)
  319. {
  320. SDL_VERSION(ver);
  321. }
  322. /* Get the library source revision */
  323. const char *
  324. SDL_GetRevision(void)
  325. {
  326. return SDL_REVISION;
  327. }
  328. /* Get the library source revision number */
  329. int
  330. SDL_GetRevisionNumber(void)
  331. {
  332. return SDL_REVISION_NUMBER;
  333. }
  334. /* Get the name of the platform */
  335. const char *
  336. SDL_GetPlatform()
  337. {
  338. #if __AIX__
  339. return "AIX";
  340. #elif __ANDROID__
  341. return "Android";
  342. #elif __BEOS__
  343. return "BeOS";
  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: */