SDL_internal.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #ifndef SDL_internal_h_
  19. #define SDL_internal_h_
  20. // Many of SDL's features require _GNU_SOURCE on various platforms
  21. #ifndef _GNU_SOURCE
  22. #define _GNU_SOURCE
  23. #endif
  24. // Need this so Linux systems define fseek64o, ftell64o and off64_t
  25. #ifndef _LARGEFILE64_SOURCE
  26. #define _LARGEFILE64_SOURCE 1
  27. #endif
  28. /* This is for a variable-length array at the end of a struct:
  29. struct x { int y; char z[SDL_VARIABLE_LENGTH_ARRAY]; };
  30. Use this because GCC 2 needs different magic than other compilers. */
  31. #if (defined(__GNUC__) && (__GNUC__ <= 2)) || defined(__CC_ARM) || defined(__cplusplus)
  32. #define SDL_VARIABLE_LENGTH_ARRAY 1
  33. #else
  34. #define SDL_VARIABLE_LENGTH_ARRAY
  35. #endif
  36. #if (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))) || defined(__clang__)
  37. #define HAVE_GCC_DIAGNOSTIC_PRAGMA 1
  38. #endif
  39. #ifdef _MSC_VER // We use constant comparison for generated code
  40. #pragma warning(disable : 6326)
  41. #endif
  42. #ifdef _MSC_VER // SDL_MAX_SMALL_ALLOC_STACKSIZE is smaller than _ALLOCA_S_THRESHOLD and should be generally safe
  43. #pragma warning(disable : 6255)
  44. #endif
  45. #define SDL_MAX_SMALL_ALLOC_STACKSIZE 128
  46. #define SDL_small_alloc(type, count, pisstack) ((*(pisstack) = ((sizeof(type) * (count)) < SDL_MAX_SMALL_ALLOC_STACKSIZE)), (*(pisstack) ? SDL_stack_alloc(type, count) : (type *)SDL_malloc(sizeof(type) * (count))))
  47. #define SDL_small_free(ptr, isstack) \
  48. if ((isstack)) { \
  49. SDL_stack_free(ptr); \
  50. } else { \
  51. SDL_free(ptr); \
  52. }
  53. #include "SDL_build_config.h"
  54. #include "dynapi/SDL_dynapi.h"
  55. #include "dynapi/SDL_dynapi_dlopennote.h"
  56. #if SDL_DYNAMIC_API
  57. #include "dynapi/SDL_dynapi_overrides.h"
  58. /* force SDL_DECLSPEC off...it's all internal symbols now.
  59. These will have actual #defines during SDL_dynapi.c only */
  60. #ifdef SDL_DECLSPEC
  61. #undef SDL_DECLSPEC
  62. #endif
  63. #define SDL_DECLSPEC
  64. #endif
  65. #ifdef SDL_PLATFORM_APPLE
  66. #ifndef _DARWIN_C_SOURCE
  67. #define _DARWIN_C_SOURCE 1 // for memset_pattern4()
  68. #endif
  69. #include <Availability.h>
  70. #ifndef __IPHONE_OS_VERSION_MAX_ALLOWED
  71. #define __IPHONE_OS_VERSION_MAX_ALLOWED 0
  72. #endif
  73. #ifndef __APPLETV_OS_VERSION_MAX_ALLOWED
  74. #define __APPLETV_OS_VERSION_MAX_ALLOWED 0
  75. #endif
  76. #ifndef __MAC_OS_X_VERSION_MAX_ALLOWED
  77. #define __MAC_OS_X_VERSION_MAX_ALLOWED 0
  78. #endif
  79. #endif // SDL_PLATFORM_APPLE
  80. #ifdef HAVE_SYS_TYPES_H
  81. #include <sys/types.h>
  82. #endif
  83. #ifdef HAVE_STDIO_H
  84. #include <stdio.h>
  85. #endif
  86. #ifdef HAVE_STDLIB_H
  87. #include <stdlib.h>
  88. #elif defined(HAVE_MALLOC_H)
  89. #include <malloc.h>
  90. #endif
  91. #ifdef HAVE_STDDEF_H
  92. #include <stddef.h>
  93. #endif
  94. #ifdef HAVE_STDARG_H
  95. #include <stdarg.h>
  96. #endif
  97. #ifdef HAVE_STRING_H
  98. #ifdef HAVE_MEMORY_H
  99. #include <memory.h>
  100. #endif
  101. #include <string.h>
  102. #endif
  103. #ifdef HAVE_STRINGS_H
  104. #include <strings.h>
  105. #endif
  106. #ifdef HAVE_WCHAR_H
  107. #include <wchar.h>
  108. #endif
  109. #ifdef HAVE_INTTYPES_H
  110. #include <inttypes.h>
  111. #elif defined(HAVE_STDINT_H)
  112. #include <stdint.h>
  113. #endif
  114. #ifdef HAVE_MATH_H
  115. #include <math.h>
  116. #endif
  117. #ifdef HAVE_FLOAT_H
  118. #include <float.h>
  119. #endif
  120. // If you run into a warning that O_CLOEXEC is redefined, update the SDL configuration header for your platform to add HAVE_O_CLOEXEC
  121. #ifndef HAVE_O_CLOEXEC
  122. #define O_CLOEXEC 0
  123. #endif
  124. /* A few #defines to reduce SDL footprint.
  125. Only effective when library is statically linked. */
  126. /* Optimized functions from 'SDL_blit_0.c'
  127. - blit with source bits_per_pixel < 8, palette */
  128. #if !defined(SDL_HAVE_BLIT_0) && !defined(SDL_LEAN_AND_MEAN)
  129. #define SDL_HAVE_BLIT_0 1
  130. #endif
  131. /* Optimized functions from 'SDL_blit_1.c'
  132. - blit with source bytes_per_pixel == 1, palette */
  133. #if !defined(SDL_HAVE_BLIT_1) && !defined(SDL_LEAN_AND_MEAN)
  134. #define SDL_HAVE_BLIT_1 1
  135. #endif
  136. /* Optimized functions from 'SDL_blit_A.c'
  137. - blit with 'SDL_BLENDMODE_BLEND' blending mode */
  138. #if !defined(SDL_HAVE_BLIT_A) && !defined(SDL_LEAN_AND_MEAN)
  139. #define SDL_HAVE_BLIT_A 1
  140. #endif
  141. /* Optimized functions from 'SDL_blit_N.c'
  142. - blit with COLORKEY mode, or nothing */
  143. #if !defined(SDL_HAVE_BLIT_N) && !defined(SDL_LEAN_AND_MEAN)
  144. #define SDL_HAVE_BLIT_N 1
  145. #endif
  146. /* Optimized functions from 'SDL_blit_N.c'
  147. - RGB565 conversion with Lookup tables */
  148. #if !defined(SDL_HAVE_BLIT_N_RGB565) && !defined(SDL_LEAN_AND_MEAN)
  149. #define SDL_HAVE_BLIT_N_RGB565 1
  150. #endif
  151. /* Optimized functions from 'SDL_blit_AUTO.c'
  152. - blit with modulate color, modulate alpha, any blending mode
  153. - scaling or not */
  154. #if !defined(SDL_HAVE_BLIT_AUTO) && !defined(SDL_LEAN_AND_MEAN)
  155. #define SDL_HAVE_BLIT_AUTO 1
  156. #endif
  157. /* Run-Length-Encoding
  158. - SDL_SetSurfaceColorKey() called with SDL_RLEACCEL flag */
  159. #if !defined(SDL_HAVE_RLE) && !defined(SDL_LEAN_AND_MEAN)
  160. #define SDL_HAVE_RLE 1
  161. #endif
  162. /* Software SDL_Renderer
  163. - creation of software renderer
  164. - *not* general blitting functions
  165. - {blend,draw}{fillrect,line,point} internal functions */
  166. #if !defined(SDL_VIDEO_RENDER_SW) && !defined(SDL_LEAN_AND_MEAN)
  167. #define SDL_VIDEO_RENDER_SW 1
  168. #endif
  169. /* STB image conversion */
  170. #if !defined(SDL_HAVE_STB) && !defined(SDL_LEAN_AND_MEAN)
  171. #define SDL_HAVE_STB 1
  172. #endif
  173. /* YUV formats
  174. - handling of YUV surfaces
  175. - blitting and conversion functions */
  176. #if !defined(SDL_HAVE_YUV) && !defined(SDL_LEAN_AND_MEAN)
  177. #define SDL_HAVE_YUV 1
  178. #endif
  179. #ifdef SDL_CAMERA_DISABLED
  180. #undef SDL_CAMERA_DRIVER_ANDROID
  181. #undef SDL_CAMERA_DRIVER_COREMEDIA
  182. #undef SDL_CAMERA_DRIVER_DUMMY
  183. #undef SDL_CAMERA_DRIVER_EMSCRIPTEN
  184. #undef SDL_CAMERA_DRIVER_MEDIAFOUNDATION
  185. #undef SDL_CAMERA_DRIVER_PIPEWIRE
  186. #undef SDL_CAMERA_DRIVER_V4L2
  187. #undef SDL_CAMERA_DRIVER_VITA
  188. #endif
  189. #ifdef SDL_RENDER_DISABLED
  190. #undef SDL_VIDEO_RENDER_SW
  191. #undef SDL_VIDEO_RENDER_D3D
  192. #undef SDL_VIDEO_RENDER_D3D11
  193. #undef SDL_VIDEO_RENDER_D3D12
  194. #undef SDL_VIDEO_RENDER_GPU
  195. #undef SDL_VIDEO_RENDER_METAL
  196. #undef SDL_VIDEO_RENDER_OGL
  197. #undef SDL_VIDEO_RENDER_OGL_ES2
  198. #undef SDL_VIDEO_RENDER_PS2
  199. #undef SDL_VIDEO_RENDER_PSP
  200. #undef SDL_VIDEO_RENDER_VITA_GXM
  201. #undef SDL_VIDEO_RENDER_VULKAN
  202. #endif // SDL_RENDER_DISABLED
  203. #ifdef SDL_GPU_DISABLED
  204. #undef SDL_GPU_D3D12
  205. #undef SDL_GPU_METAL
  206. #undef SDL_GPU_VULKAN
  207. #undef SDL_VIDEO_RENDER_GPU
  208. #endif // SDL_GPU_DISABLED
  209. #if !defined(HAVE_LIBC)
  210. // If not using _any_ C runtime, these have to be defined before SDL_thread.h
  211. // gets included, so internal SDL_CreateThread calls will not try to reference
  212. // the (unavailable and unneeded) _beginthreadex/_endthreadex functions.
  213. #define SDL_BeginThreadFunction NULL
  214. #define SDL_EndThreadFunction NULL
  215. #endif
  216. #ifdef SDL_NOLONGLONG
  217. #error We cannot build a valid SDL3 library without long long support
  218. #endif
  219. /* Enable internal definitions in SDL API headers */
  220. #define SDL_INTERNAL
  221. #include <SDL3/SDL.h>
  222. #include <SDL3/SDL_intrin.h>
  223. #define SDL_MAIN_NOIMPL // don't drag in header-only implementation of SDL_main
  224. #include <SDL3/SDL_main.h>
  225. // Set up for C function definitions, even when using C++
  226. #ifdef __cplusplus
  227. extern "C" {
  228. #endif
  229. #include "SDL_utils_c.h"
  230. #include "SDL_hashtable.h"
  231. /* SDL_ExitProcess is not declared in any public header, although
  232. it is shared between some parts of SDL, because we don't want
  233. anything calling it without an extremely good reason. */
  234. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  235. #ifdef HAVE_LIBC
  236. #define SDL_abort() abort()
  237. #else
  238. #define SDL_abort() do { \
  239. SDL_TriggerBreakpoint(); \
  240. SDL_ExitProcess(42); \
  241. } while (0)
  242. #endif
  243. #define PUSH_SDL_ERROR() \
  244. { char *_error = SDL_strdup(SDL_GetError());
  245. #define POP_SDL_ERROR() \
  246. SDL_SetError("%s", _error); SDL_free(_error); }
  247. #if defined(SDL_DISABLE_INVALID_PARAMS)
  248. #ifdef DEBUG
  249. // If you define SDL_DISABLE_INVALID_PARAMS, you're promising that you'll
  250. // never pass an invalid parameter to SDL, since it may crash or lead to
  251. // hard to diagnose bugs. Let's assert that this is true in debug builds.
  252. #define CHECK_PARAM(invalid) SDL_assert_always(!(invalid)); if (false)
  253. #else
  254. #define CHECK_PARAM(invalid) if (false)
  255. #endif
  256. #elif defined(SDL_ASSERT_INVALID_PARAMS)
  257. #define CHECK_PARAM(invalid) SDL_assert_always(!(invalid)); if (invalid)
  258. #else
  259. #define CHECK_PARAM(invalid) if (invalid)
  260. #endif
  261. // Do any initialization that needs to happen before threads are started
  262. extern void SDL_InitMainThread(void);
  263. /* The internal implementations of these functions have up to nanosecond precision.
  264. We can expose these functions as part of the API if we want to later.
  265. */
  266. extern bool SDLCALL SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS);
  267. extern bool SDLCALL SDL_WaitConditionTimeoutNS(SDL_Condition *cond, SDL_Mutex *mutex, Sint64 timeoutNS);
  268. extern bool SDLCALL SDL_WaitEventTimeoutNS(SDL_Event *event, Sint64 timeoutNS);
  269. // Ends C function definitions when using C++
  270. #ifdef __cplusplus
  271. }
  272. #endif
  273. #endif // SDL_internal_h_