SDL_init.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. /**
  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` */
  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. * \threadsafety This function should only be called on the main thread.
  211. *
  212. * \since This function is available since SDL 3.2.0.
  213. *
  214. * \sa SDL_SetAppMetadata
  215. * \sa SDL_SetAppMetadataProperty
  216. * \sa SDL_InitSubSystem
  217. * \sa SDL_Quit
  218. * \sa SDL_SetMainReady
  219. * \sa SDL_WasInit
  220. */
  221. extern SDL_DECLSPEC bool SDLCALL SDL_Init(SDL_InitFlags flags);
  222. /**
  223. * Compatibility function to initialize the SDL library.
  224. *
  225. * This function and SDL_Init() are interchangeable.
  226. *
  227. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  228. * \returns true on success or false on failure; call SDL_GetError() for more
  229. * information.
  230. *
  231. * \threadsafety This function should only be called on the main thread.
  232. *
  233. * \since This function is available since SDL 3.2.0.
  234. *
  235. * \sa SDL_Init
  236. * \sa SDL_Quit
  237. * \sa SDL_QuitSubSystem
  238. */
  239. extern SDL_DECLSPEC bool SDLCALL SDL_InitSubSystem(SDL_InitFlags flags);
  240. /**
  241. * Shut down specific SDL subsystems.
  242. *
  243. * You still need to call SDL_Quit() even if you close all open subsystems
  244. * with SDL_QuitSubSystem().
  245. *
  246. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  247. *
  248. * \threadsafety This function is not thread safe.
  249. *
  250. * \since This function is available since SDL 3.2.0.
  251. *
  252. * \sa SDL_InitSubSystem
  253. * \sa SDL_Quit
  254. */
  255. extern SDL_DECLSPEC void SDLCALL SDL_QuitSubSystem(SDL_InitFlags flags);
  256. /**
  257. * Get a mask of the specified subsystems which are currently initialized.
  258. *
  259. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  260. * \returns a mask of all initialized subsystems if `flags` is 0, otherwise it
  261. * returns the initialization status of the specified subsystems.
  262. *
  263. * \threadsafety This function is not thread safe.
  264. *
  265. * \since This function is available since SDL 3.2.0.
  266. *
  267. * \sa SDL_Init
  268. * \sa SDL_InitSubSystem
  269. */
  270. extern SDL_DECLSPEC SDL_InitFlags SDLCALL SDL_WasInit(SDL_InitFlags flags);
  271. /**
  272. * Clean up all initialized subsystems.
  273. *
  274. * You should call this function even if you have already shutdown each
  275. * initialized subsystem with SDL_QuitSubSystem(). It is safe to call this
  276. * function even in the case of errors in initialization.
  277. *
  278. * You can use this function with atexit() to ensure that it is run when your
  279. * application is shutdown, but it is not wise to do this from a library or
  280. * other dynamically loaded code.
  281. *
  282. * \threadsafety This function should only be called on the main thread.
  283. *
  284. * \since This function is available since SDL 3.2.0.
  285. *
  286. * \sa SDL_Init
  287. * \sa SDL_QuitSubSystem
  288. */
  289. extern SDL_DECLSPEC void SDLCALL SDL_Quit(void);
  290. /**
  291. * Return whether this is the main thread.
  292. *
  293. * On Apple platforms, the main thread is the thread that runs your program's
  294. * main() entry point. On other platforms, the main thread is the one that
  295. * calls SDL_Init(SDL_INIT_VIDEO), which should usually be the one that runs
  296. * your program's main() entry point. If you are using the main callbacks,
  297. * SDL_AppInit(), SDL_AppIterate(), and SDL_AppQuit() are all called on the
  298. * main thread.
  299. *
  300. * \returns true if this thread is the main thread, or false otherwise.
  301. *
  302. * \threadsafety It is safe to call this function from any thread.
  303. *
  304. * \since This function is available since SDL 3.2.0.
  305. *
  306. * \sa SDL_RunOnMainThread
  307. */
  308. extern SDL_DECLSPEC bool SDLCALL SDL_IsMainThread(void);
  309. /**
  310. * Callback run on the main thread.
  311. *
  312. * \param userdata an app-controlled pointer that is passed to the callback.
  313. *
  314. * \since This datatype is available since SDL 3.2.0.
  315. *
  316. * \sa SDL_RunOnMainThread
  317. */
  318. typedef void (SDLCALL *SDL_MainThreadCallback)(void *userdata);
  319. /**
  320. * Call a function on the main thread during event processing.
  321. *
  322. * If this is called on the main thread, the callback is executed immediately.
  323. * If this is called on another thread, this callback is queued for execution
  324. * on the main thread during event processing.
  325. *
  326. * Be careful of deadlocks when using this functionality. You should not have
  327. * the main thread wait for the current thread while this function is being
  328. * called with `wait_complete` true.
  329. *
  330. * \param callback the callback to call on the main thread.
  331. * \param userdata a pointer that is passed to `callback`.
  332. * \param wait_complete true to wait for the callback to complete, false to
  333. * return immediately.
  334. * \returns true on success or false on failure; call SDL_GetError() for more
  335. * information.
  336. *
  337. * \threadsafety It is safe to call this function from any thread.
  338. *
  339. * \since This function is available since SDL 3.2.0.
  340. *
  341. * \sa SDL_IsMainThread
  342. */
  343. extern SDL_DECLSPEC bool SDLCALL SDL_RunOnMainThread(SDL_MainThreadCallback callback, void *userdata, bool wait_complete);
  344. /**
  345. * Specify basic metadata about your app.
  346. *
  347. * You can optionally provide metadata about your app to SDL. This is not
  348. * required, but strongly encouraged.
  349. *
  350. * There are several locations where SDL can make use of metadata (an "About"
  351. * box in the macOS menu bar, the name of the app can be shown on some audio
  352. * mixers, etc). Any piece of metadata can be left as NULL, if a specific
  353. * detail doesn't make sense for the app.
  354. *
  355. * This function should be called as early as possible, before SDL_Init.
  356. * Multiple calls to this function are allowed, but various state might not
  357. * change once it has been set up with a previous call to this function.
  358. *
  359. * Passing a NULL removes any previous metadata.
  360. *
  361. * This is a simplified interface for the most important information. You can
  362. * supply significantly more detailed metadata with
  363. * SDL_SetAppMetadataProperty().
  364. *
  365. * \param appname The name of the application ("My Game 2: Bad Guy's
  366. * Revenge!").
  367. * \param appversion The version of the application ("1.0.0beta5" or a git
  368. * hash, or whatever makes sense).
  369. * \param appidentifier A unique string in reverse-domain format that
  370. * identifies this app ("com.example.mygame2").
  371. * \returns true on success or false on failure; call SDL_GetError() for more
  372. * information.
  373. *
  374. * \threadsafety It is safe to call this function from any thread.
  375. *
  376. * \since This function is available since SDL 3.2.0.
  377. *
  378. * \sa SDL_SetAppMetadataProperty
  379. */
  380. extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier);
  381. /**
  382. * Specify metadata about your app through a set of properties.
  383. *
  384. * You can optionally provide metadata about your app to SDL. This is not
  385. * required, but strongly encouraged.
  386. *
  387. * There are several locations where SDL can make use of metadata (an "About"
  388. * box in the macOS menu bar, the name of the app can be shown on some audio
  389. * mixers, etc). Any piece of metadata can be left out, if a specific detail
  390. * doesn't make sense for the app.
  391. *
  392. * This function should be called as early as possible, before SDL_Init.
  393. * Multiple calls to this function are allowed, but various state might not
  394. * change once it has been set up with a previous call to this function.
  395. *
  396. * Once set, this metadata can be read using SDL_GetAppMetadataProperty().
  397. *
  398. * These are the supported properties:
  399. *
  400. * - `SDL_PROP_APP_METADATA_NAME_STRING`: The human-readable name of the
  401. * application, like "My Game 2: Bad Guy's Revenge!". This will show up
  402. * anywhere the OS shows the name of the application separately from window
  403. * titles, such as volume control applets, etc. This defaults to "SDL
  404. * Application".
  405. * - `SDL_PROP_APP_METADATA_VERSION_STRING`: The version of the app that is
  406. * running; there are no rules on format, so "1.0.3beta2" and "April 22nd,
  407. * 2024" and a git hash are all valid options. This has no default.
  408. * - `SDL_PROP_APP_METADATA_IDENTIFIER_STRING`: A unique string that
  409. * identifies this app. This must be in reverse-domain format, like
  410. * "com.example.mygame2". This string is used by desktop compositors to
  411. * identify and group windows together, as well as match applications with
  412. * associated desktop settings and icons. If you plan to package your
  413. * application in a container such as Flatpak, the app ID should match the
  414. * name of your Flatpak container as well. This has no default.
  415. * - `SDL_PROP_APP_METADATA_CREATOR_STRING`: The human-readable name of the
  416. * creator/developer/maker of this app, like "MojoWorkshop, LLC"
  417. * - `SDL_PROP_APP_METADATA_COPYRIGHT_STRING`: The human-readable copyright
  418. * notice, like "Copyright (c) 2024 MojoWorkshop, LLC" or whatnot. Keep this
  419. * to one line, don't paste a copy of a whole software license in here. This
  420. * has no default.
  421. * - `SDL_PROP_APP_METADATA_URL_STRING`: A URL to the app on the web. Maybe a
  422. * product page, or a storefront, or even a GitHub repository, for user's
  423. * further information This has no default.
  424. * - `SDL_PROP_APP_METADATA_TYPE_STRING`: The type of application this is.
  425. * Currently this string can be "game" for a video game, "mediaplayer" for a
  426. * media player, or generically "application" if nothing else applies.
  427. * Future versions of SDL might add new types. This defaults to
  428. * "application".
  429. *
  430. * \param name the name of the metadata property to set.
  431. * \param value the value of the property, or NULL to remove that property.
  432. * \returns true on success or false on failure; call SDL_GetError() for more
  433. * information.
  434. *
  435. * \threadsafety It is safe to call this function from any thread.
  436. *
  437. * \since This function is available since SDL 3.2.0.
  438. *
  439. * \sa SDL_GetAppMetadataProperty
  440. * \sa SDL_SetAppMetadata
  441. */
  442. extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadataProperty(const char *name, const char *value);
  443. #define SDL_PROP_APP_METADATA_NAME_STRING "SDL.app.metadata.name"
  444. #define SDL_PROP_APP_METADATA_VERSION_STRING "SDL.app.metadata.version"
  445. #define SDL_PROP_APP_METADATA_IDENTIFIER_STRING "SDL.app.metadata.identifier"
  446. #define SDL_PROP_APP_METADATA_CREATOR_STRING "SDL.app.metadata.creator"
  447. #define SDL_PROP_APP_METADATA_COPYRIGHT_STRING "SDL.app.metadata.copyright"
  448. #define SDL_PROP_APP_METADATA_URL_STRING "SDL.app.metadata.url"
  449. #define SDL_PROP_APP_METADATA_TYPE_STRING "SDL.app.metadata.type"
  450. /**
  451. * Get metadata about your app.
  452. *
  453. * This returns metadata previously set using SDL_SetAppMetadata() or
  454. * SDL_SetAppMetadataProperty(). See SDL_SetAppMetadataProperty() for the list
  455. * of available properties and their meanings.
  456. *
  457. * \param name the name of the metadata property to get.
  458. * \returns the current value of the metadata property, or the default if it
  459. * is not set, NULL for properties with no default.
  460. *
  461. * \threadsafety It is safe to call this function from any thread, although
  462. * the string returned is not protected and could potentially be
  463. * freed if you call SDL_SetAppMetadataProperty() to set that
  464. * property from another thread.
  465. *
  466. * \since This function is available since SDL 3.2.0.
  467. *
  468. * \sa SDL_SetAppMetadata
  469. * \sa SDL_SetAppMetadataProperty
  470. */
  471. extern SDL_DECLSPEC const char * SDLCALL SDL_GetAppMetadataProperty(const char *name);
  472. /* Ends C function definitions when using C++ */
  473. #ifdef __cplusplus
  474. }
  475. #endif
  476. #include <SDL3/SDL_close_code.h>
  477. #endif /* SDL_init_h_ */