SDL_init.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. /**
  19. * # CategoryInit
  20. *
  21. * All SDL programs need to initialize the library before starting to work
  22. * with it.
  23. *
  24. * Almost everything can simply call SDL_Init() near startup, with a handful
  25. * of flags to specify subsystems to touch. These are here to make sure SDL
  26. * does not even attempt to touch low-level pieces of the operating system
  27. * that you don't intend to use. For example, you might be using SDL for video
  28. * and input but chose an external library for audio, and in this case you
  29. * would just need to leave off the `SDL_INIT_AUDIO` flag to make sure that
  30. * external library has complete control.
  31. *
  32. * Most apps, when terminating, should call SDL_Quit(). This will clean up
  33. * (nearly) everything that SDL might have allocated, and crucially, it'll
  34. * make sure that the display's resolution is back to what the user expects if
  35. * you had previously changed it for your game.
  36. *
  37. * SDL3 apps are strongly encouraged to call SDL_SetAppMetadata() at startup
  38. * to fill in details about the program. This is completely optional, but it
  39. * helps in small ways (we can provide an About dialog box for the macOS menu,
  40. * we can name the app in the system's audio mixer, etc). Those that want to
  41. * provide a _lot_ of information should look at the more-detailed
  42. * SDL_SetAppMetadataProperty().
  43. */
  44. #ifndef SDL_init_h_
  45. #define SDL_init_h_
  46. #include <SDL3/SDL_stdinc.h>
  47. #include <SDL3/SDL_error.h>
  48. #include <SDL3/SDL_events.h>
  49. #include <SDL3/SDL_begin_code.h>
  50. /* Set up for C function definitions, even when using C++ */
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /* As of version 0.5, SDL is loaded dynamically into the application */
  55. /**
  56. * Initialization flags for SDL_Init and/or SDL_InitSubSystem
  57. *
  58. * These are the flags which may be passed to SDL_Init(). You should specify
  59. * the subsystems which you will be using in your application.
  60. *
  61. * \since This datatype is available since SDL 3.2.0.
  62. *
  63. * \sa SDL_Init
  64. * \sa SDL_Quit
  65. * \sa SDL_InitSubSystem
  66. * \sa SDL_QuitSubSystem
  67. * \sa SDL_WasInit
  68. */
  69. typedef Uint32 SDL_InitFlags;
  70. #define SDL_INIT_AUDIO 0x00000010u /**< `SDL_INIT_AUDIO` implies `SDL_INIT_EVENTS` */
  71. #define SDL_INIT_VIDEO 0x00000020u /**< `SDL_INIT_VIDEO` implies `SDL_INIT_EVENTS`, should be initialized on the main thread */
  72. #define SDL_INIT_JOYSTICK 0x00000200u /**< `SDL_INIT_JOYSTICK` implies `SDL_INIT_EVENTS`, should be initialized on the same thread as SDL_INIT_VIDEO on Windows if you don't set SDL_HINT_JOYSTICK_THREAD */
  73. #define SDL_INIT_HAPTIC 0x00001000u
  74. #define SDL_INIT_GAMEPAD 0x00002000u /**< `SDL_INIT_GAMEPAD` implies `SDL_INIT_JOYSTICK` */
  75. #define SDL_INIT_EVENTS 0x00004000u
  76. #define SDL_INIT_SENSOR 0x00008000u /**< `SDL_INIT_SENSOR` implies `SDL_INIT_EVENTS` */
  77. #define SDL_INIT_CAMERA 0x00010000u /**< `SDL_INIT_CAMERA` implies `SDL_INIT_EVENTS` */
  78. /**
  79. * Return values for optional main callbacks.
  80. *
  81. * Returning SDL_APP_SUCCESS or SDL_APP_FAILURE from SDL_AppInit,
  82. * SDL_AppEvent, or SDL_AppIterate will terminate the program and report
  83. * success/failure to the operating system. What that means is
  84. * platform-dependent. On Unix, for example, on success, the process error
  85. * code will be zero, and on failure it will be 1. This interface doesn't
  86. * allow you to return specific exit codes, just whether there was an error
  87. * generally or not.
  88. *
  89. * Returning SDL_APP_CONTINUE from these functions will let the app continue
  90. * to run.
  91. *
  92. * See
  93. * [Main callbacks in SDL3](https://wiki.libsdl.org/SDL3/README/main-functions#main-callbacks-in-sdl3)
  94. * for complete details.
  95. *
  96. * \since This enum is available since SDL 3.2.0.
  97. */
  98. typedef enum SDL_AppResult
  99. {
  100. SDL_APP_CONTINUE, /**< Value that requests that the app continue from the main callbacks. */
  101. SDL_APP_SUCCESS, /**< Value that requests termination with success from the main callbacks. */
  102. SDL_APP_FAILURE /**< Value that requests termination with error from the main callbacks. */
  103. } SDL_AppResult;
  104. /**
  105. * Function pointer typedef for SDL_AppInit.
  106. *
  107. * These are used by SDL_EnterAppMainCallbacks. This mechanism operates behind
  108. * the scenes for apps using the optional main callbacks. Apps that want to
  109. * use this should just implement SDL_AppInit directly.
  110. *
  111. * \param appstate a place where the app can optionally store a pointer for
  112. * future use.
  113. * \param argc the standard ANSI C main's argc; number of elements in `argv`.
  114. * \param argv the standard ANSI C main's argv; array of command line
  115. * arguments.
  116. * \returns SDL_APP_FAILURE to terminate with an error, SDL_APP_SUCCESS to
  117. * terminate with success, SDL_APP_CONTINUE to continue.
  118. *
  119. * \since This datatype is available since SDL 3.2.0.
  120. */
  121. typedef SDL_AppResult (SDLCALL *SDL_AppInit_func)(void **appstate, int argc, char *argv[]);
  122. /**
  123. * Function pointer typedef for SDL_AppIterate.
  124. *
  125. * These are used by SDL_EnterAppMainCallbacks. This mechanism operates behind
  126. * the scenes for apps using the optional main callbacks. Apps that want to
  127. * use this should just implement SDL_AppIterate directly.
  128. *
  129. * \param appstate an optional pointer, provided by the app in SDL_AppInit.
  130. * \returns SDL_APP_FAILURE to terminate with an error, SDL_APP_SUCCESS to
  131. * terminate with success, SDL_APP_CONTINUE to continue.
  132. *
  133. * \since This datatype is available since SDL 3.2.0.
  134. */
  135. typedef SDL_AppResult (SDLCALL *SDL_AppIterate_func)(void *appstate);
  136. /**
  137. * Function pointer typedef for SDL_AppEvent.
  138. *
  139. * These are used by SDL_EnterAppMainCallbacks. This mechanism operates behind
  140. * the scenes for apps using the optional main callbacks. Apps that want to
  141. * use this should just implement SDL_AppEvent directly.
  142. *
  143. * \param appstate an optional pointer, provided by the app in SDL_AppInit.
  144. * \param event the new event for the app to examine.
  145. * \returns SDL_APP_FAILURE to terminate with an error, SDL_APP_SUCCESS to
  146. * terminate with success, SDL_APP_CONTINUE to continue.
  147. *
  148. * \since This datatype is available since SDL 3.2.0.
  149. */
  150. typedef SDL_AppResult (SDLCALL *SDL_AppEvent_func)(void *appstate, SDL_Event *event);
  151. /**
  152. * Function pointer typedef for SDL_AppQuit.
  153. *
  154. * These are used by SDL_EnterAppMainCallbacks. This mechanism operates behind
  155. * the scenes for apps using the optional main callbacks. Apps that want to
  156. * use this should just implement SDL_AppEvent directly.
  157. *
  158. * \param appstate an optional pointer, provided by the app in SDL_AppInit.
  159. * \param result the result code that terminated the app (success or failure).
  160. *
  161. * \since This datatype is available since SDL 3.2.0.
  162. */
  163. typedef void (SDLCALL *SDL_AppQuit_func)(void *appstate, SDL_AppResult result);
  164. /**
  165. * Initialize the SDL library.
  166. *
  167. * SDL_Init() simply forwards to calling SDL_InitSubSystem(). Therefore, the
  168. * two may be used interchangeably. Though for readability of your code
  169. * SDL_InitSubSystem() might be preferred.
  170. *
  171. * The file I/O (for example: SDL_IOFromFile) and threading (SDL_CreateThread)
  172. * subsystems are initialized by default. Message boxes
  173. * (SDL_ShowSimpleMessageBox) also attempt to work without initializing the
  174. * video subsystem, in hopes of being useful in showing an error dialog when
  175. * SDL_Init fails. You must specifically initialize other subsystems if you
  176. * use them in your application.
  177. *
  178. * Logging (such as SDL_Log) works without initialization, too.
  179. *
  180. * `flags` may be any of the following OR'd together:
  181. *
  182. * - `SDL_INIT_AUDIO`: audio subsystem; automatically initializes the events
  183. * subsystem
  184. * - `SDL_INIT_VIDEO`: video subsystem; automatically initializes the events
  185. * subsystem, should be initialized on the main thread.
  186. * - `SDL_INIT_JOYSTICK`: joystick subsystem; automatically initializes the
  187. * events subsystem
  188. * - `SDL_INIT_HAPTIC`: haptic (force feedback) subsystem
  189. * - `SDL_INIT_GAMEPAD`: gamepad subsystem; automatically initializes the
  190. * joystick subsystem
  191. * - `SDL_INIT_EVENTS`: events subsystem
  192. * - `SDL_INIT_SENSOR`: sensor subsystem; automatically initializes the events
  193. * subsystem
  194. * - `SDL_INIT_CAMERA`: camera subsystem; automatically initializes the events
  195. * subsystem
  196. *
  197. * Subsystem initialization is ref-counted, you must call SDL_QuitSubSystem()
  198. * for each SDL_InitSubSystem() to correctly shutdown a subsystem manually (or
  199. * call SDL_Quit() to force shutdown). If a subsystem is already loaded then
  200. * this call will increase the ref-count and return.
  201. *
  202. * Consider reporting some basic metadata about your application before
  203. * calling SDL_Init, using either SDL_SetAppMetadata() or
  204. * SDL_SetAppMetadataProperty().
  205. *
  206. * \param flags subsystem initialization flags.
  207. * \returns true on success or false on failure; call SDL_GetError() for more
  208. * information.
  209. *
  210. * \since This function is available since SDL 3.2.0.
  211. *
  212. * \sa SDL_SetAppMetadata
  213. * \sa SDL_SetAppMetadataProperty
  214. * \sa SDL_InitSubSystem
  215. * \sa SDL_Quit
  216. * \sa SDL_SetMainReady
  217. * \sa SDL_WasInit
  218. */
  219. extern SDL_DECLSPEC bool SDLCALL SDL_Init(SDL_InitFlags flags);
  220. /**
  221. * Compatibility function to initialize the SDL library.
  222. *
  223. * This function and SDL_Init() are interchangeable.
  224. *
  225. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  226. * \returns true on success or false on failure; call SDL_GetError() for more
  227. * information.
  228. *
  229. * \since This function is available since SDL 3.2.0.
  230. *
  231. * \sa SDL_Init
  232. * \sa SDL_Quit
  233. * \sa SDL_QuitSubSystem
  234. */
  235. extern SDL_DECLSPEC bool SDLCALL SDL_InitSubSystem(SDL_InitFlags flags);
  236. /**
  237. * Shut down specific SDL subsystems.
  238. *
  239. * You still need to call SDL_Quit() even if you close all open subsystems
  240. * with SDL_QuitSubSystem().
  241. *
  242. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  243. *
  244. * \since This function is available since SDL 3.2.0.
  245. *
  246. * \sa SDL_InitSubSystem
  247. * \sa SDL_Quit
  248. */
  249. extern SDL_DECLSPEC void SDLCALL SDL_QuitSubSystem(SDL_InitFlags flags);
  250. /**
  251. * Get a mask of the specified subsystems which are currently initialized.
  252. *
  253. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  254. * \returns a mask of all initialized subsystems if `flags` is 0, otherwise it
  255. * returns the initialization status of the specified subsystems.
  256. *
  257. * \since This function is available since SDL 3.2.0.
  258. *
  259. * \sa SDL_Init
  260. * \sa SDL_InitSubSystem
  261. */
  262. extern SDL_DECLSPEC SDL_InitFlags SDLCALL SDL_WasInit(SDL_InitFlags flags);
  263. /**
  264. * Clean up all initialized subsystems.
  265. *
  266. * You should call this function even if you have already shutdown each
  267. * initialized subsystem with SDL_QuitSubSystem(). It is safe to call this
  268. * function even in the case of errors in initialization.
  269. *
  270. * You can use this function with atexit() to ensure that it is run when your
  271. * application is shutdown, but it is not wise to do this from a library or
  272. * other dynamically loaded code.
  273. *
  274. * \since This function is available since SDL 3.2.0.
  275. *
  276. * \sa SDL_Init
  277. * \sa SDL_QuitSubSystem
  278. */
  279. extern SDL_DECLSPEC void SDLCALL SDL_Quit(void);
  280. /**
  281. * Return whether this is the main thread.
  282. *
  283. * On Apple platforms, the main thread is the thread that runs your program's
  284. * main() entry point. On other platforms, the main thread is the one that
  285. * calls SDL_Init(SDL_INIT_VIDEO), which should usually be the one that runs
  286. * your program's main() entry point. If you are using the main callbacks,
  287. * SDL_AppInit(), SDL_AppIterate(), and SDL_AppQuit() are all called on the
  288. * main thread.
  289. *
  290. * \returns true if this thread is the main thread, or false otherwise.
  291. *
  292. * \threadsafety It is safe to call this function from any thread.
  293. *
  294. * \since This function is available since SDL 3.2.0.
  295. *
  296. * \sa SDL_RunOnMainThread
  297. */
  298. extern SDL_DECLSPEC bool SDLCALL SDL_IsMainThread(void);
  299. /**
  300. * Callback run on the main thread.
  301. *
  302. * \param userdata an app-controlled pointer that is passed to the callback.
  303. *
  304. * \since This datatype is available since SDL 3.2.0.
  305. *
  306. * \sa SDL_RunOnMainThread
  307. */
  308. typedef void (SDLCALL *SDL_MainThreadCallback)(void *userdata);
  309. /**
  310. * Call a function on the main thread during event processing.
  311. *
  312. * If this is called on the main thread, the callback is executed immediately.
  313. * If this is called on another thread, this callback is queued for execution
  314. * on the main thread during event processing.
  315. *
  316. * Be careful of deadlocks when using this functionality. You should not have
  317. * the main thread wait for the current thread while this function is being
  318. * called with `wait_complete` true.
  319. *
  320. * \param callback the callback to call on the main thread.
  321. * \param userdata a pointer that is passed to `callback`.
  322. * \param wait_complete true to wait for the callback to complete, false to
  323. * return immediately.
  324. * \returns true on success or false on failure; call SDL_GetError() for more
  325. * information.
  326. *
  327. * \threadsafety It is safe to call this function from any thread.
  328. *
  329. * \since This function is available since SDL 3.2.0.
  330. *
  331. * \sa SDL_IsMainThread
  332. */
  333. extern SDL_DECLSPEC bool SDLCALL SDL_RunOnMainThread(SDL_MainThreadCallback callback, void *userdata, bool wait_complete);
  334. /**
  335. * Specify basic metadata about your app.
  336. *
  337. * You can optionally provide metadata about your app to SDL. This is not
  338. * required, but strongly encouraged.
  339. *
  340. * There are several locations where SDL can make use of metadata (an "About"
  341. * box in the macOS menu bar, the name of the app can be shown on some audio
  342. * mixers, etc). Any piece of metadata can be left as NULL, if a specific
  343. * detail doesn't make sense for the app.
  344. *
  345. * This function should be called as early as possible, before SDL_Init.
  346. * Multiple calls to this function are allowed, but various state might not
  347. * change once it has been set up with a previous call to this function.
  348. *
  349. * Passing a NULL removes any previous metadata.
  350. *
  351. * This is a simplified interface for the most important information. You can
  352. * supply significantly more detailed metadata with
  353. * SDL_SetAppMetadataProperty().
  354. *
  355. * \param appname The name of the application ("My Game 2: Bad Guy's
  356. * Revenge!").
  357. * \param appversion The version of the application ("1.0.0beta5" or a git
  358. * hash, or whatever makes sense).
  359. * \param appidentifier A unique string in reverse-domain format that
  360. * identifies this app ("com.example.mygame2").
  361. * \returns true on success or false on failure; call SDL_GetError() for more
  362. * information.
  363. *
  364. * \threadsafety It is safe to call this function from any thread.
  365. *
  366. * \since This function is available since SDL 3.2.0.
  367. *
  368. * \sa SDL_SetAppMetadataProperty
  369. */
  370. extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier);
  371. /**
  372. * Specify metadata about your app through a set of properties.
  373. *
  374. * You can optionally provide metadata about your app to SDL. This is not
  375. * required, but strongly encouraged.
  376. *
  377. * There are several locations where SDL can make use of metadata (an "About"
  378. * box in the macOS menu bar, the name of the app can be shown on some audio
  379. * mixers, etc). Any piece of metadata can be left out, if a specific detail
  380. * doesn't make sense for the app.
  381. *
  382. * This function should be called as early as possible, before SDL_Init.
  383. * Multiple calls to this function are allowed, but various state might not
  384. * change once it has been set up with a previous call to this function.
  385. *
  386. * Once set, this metadata can be read using SDL_GetAppMetadataProperty().
  387. *
  388. * These are the supported properties:
  389. *
  390. * - `SDL_PROP_APP_METADATA_NAME_STRING`: The human-readable name of the
  391. * application, like "My Game 2: Bad Guy's Revenge!". This will show up
  392. * anywhere the OS shows the name of the application separately from window
  393. * titles, such as volume control applets, etc. This defaults to "SDL
  394. * Application".
  395. * - `SDL_PROP_APP_METADATA_VERSION_STRING`: The version of the app that is
  396. * running; there are no rules on format, so "1.0.3beta2" and "April 22nd,
  397. * 2024" and a git hash are all valid options. This has no default.
  398. * - `SDL_PROP_APP_METADATA_IDENTIFIER_STRING`: A unique string that
  399. * identifies this app. This must be in reverse-domain format, like
  400. * "com.example.mygame2". This string is used by desktop compositors to
  401. * identify and group windows together, as well as match applications with
  402. * associated desktop settings and icons. If you plan to package your
  403. * application in a container such as Flatpak, the app ID should match the
  404. * name of your Flatpak container as well. This has no default.
  405. * - `SDL_PROP_APP_METADATA_CREATOR_STRING`: The human-readable name of the
  406. * creator/developer/maker of this app, like "MojoWorkshop, LLC"
  407. * - `SDL_PROP_APP_METADATA_COPYRIGHT_STRING`: The human-readable copyright
  408. * notice, like "Copyright (c) 2024 MojoWorkshop, LLC" or whatnot. Keep this
  409. * to one line, don't paste a copy of a whole software license in here. This
  410. * has no default.
  411. * - `SDL_PROP_APP_METADATA_URL_STRING`: A URL to the app on the web. Maybe a
  412. * product page, or a storefront, or even a GitHub repository, for user's
  413. * further information This has no default.
  414. * - `SDL_PROP_APP_METADATA_TYPE_STRING`: The type of application this is.
  415. * Currently this string can be "game" for a video game, "mediaplayer" for a
  416. * media player, or generically "application" if nothing else applies.
  417. * Future versions of SDL might add new types. This defaults to
  418. * "application".
  419. *
  420. * \param name the name of the metadata property to set.
  421. * \param value the value of the property, or NULL to remove that property.
  422. * \returns true on success or false on failure; call SDL_GetError() for more
  423. * information.
  424. *
  425. * \threadsafety It is safe to call this function from any thread.
  426. *
  427. * \since This function is available since SDL 3.2.0.
  428. *
  429. * \sa SDL_GetAppMetadataProperty
  430. * \sa SDL_SetAppMetadata
  431. */
  432. extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadataProperty(const char *name, const char *value);
  433. #define SDL_PROP_APP_METADATA_NAME_STRING "SDL.app.metadata.name"
  434. #define SDL_PROP_APP_METADATA_VERSION_STRING "SDL.app.metadata.version"
  435. #define SDL_PROP_APP_METADATA_IDENTIFIER_STRING "SDL.app.metadata.identifier"
  436. #define SDL_PROP_APP_METADATA_CREATOR_STRING "SDL.app.metadata.creator"
  437. #define SDL_PROP_APP_METADATA_COPYRIGHT_STRING "SDL.app.metadata.copyright"
  438. #define SDL_PROP_APP_METADATA_URL_STRING "SDL.app.metadata.url"
  439. #define SDL_PROP_APP_METADATA_TYPE_STRING "SDL.app.metadata.type"
  440. /**
  441. * Get metadata about your app.
  442. *
  443. * This returns metadata previously set using SDL_SetAppMetadata() or
  444. * SDL_SetAppMetadataProperty(). See SDL_SetAppMetadataProperty() for the list
  445. * of available properties and their meanings.
  446. *
  447. * \param name the name of the metadata property to get.
  448. * \returns the current value of the metadata property, or the default if it
  449. * is not set, NULL for properties with no default.
  450. *
  451. * \threadsafety It is safe to call this function from any thread, although
  452. * the string returned is not protected and could potentially be
  453. * freed if you call SDL_SetAppMetadataProperty() to set that
  454. * property from another thread.
  455. *
  456. * \since This function is available since SDL 3.2.0.
  457. *
  458. * \sa SDL_SetAppMetadata
  459. * \sa SDL_SetAppMetadataProperty
  460. */
  461. extern SDL_DECLSPEC const char * SDLCALL SDL_GetAppMetadataProperty(const char *name);
  462. /* Ends C function definitions when using C++ */
  463. #ifdef __cplusplus
  464. }
  465. #endif
  466. #include <SDL3/SDL_close_code.h>
  467. #endif /* SDL_init_h_ */