SDL.c 11 KB

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