SDL_init.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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.0.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` */
  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.0.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. typedef SDL_AppResult (SDLCALL *SDL_AppInit_func)(void **appstate, int argc, char *argv[]);
  105. typedef SDL_AppResult (SDLCALL *SDL_AppIterate_func)(void *appstate);
  106. typedef SDL_AppResult (SDLCALL *SDL_AppEvent_func)(void *appstate, SDL_Event *event);
  107. typedef void (SDLCALL *SDL_AppQuit_func)(void *appstate, SDL_AppResult result);
  108. /**
  109. * Initialize the SDL library.
  110. *
  111. * SDL_Init() simply forwards to calling SDL_InitSubSystem(). Therefore, the
  112. * two may be used interchangeably. Though for readability of your code
  113. * SDL_InitSubSystem() might be preferred.
  114. *
  115. * The file I/O (for example: SDL_IOFromFile) and threading (SDL_CreateThread)
  116. * subsystems are initialized by default. Message boxes
  117. * (SDL_ShowSimpleMessageBox) also attempt to work without initializing the
  118. * video subsystem, in hopes of being useful in showing an error dialog when
  119. * SDL_Init fails. You must specifically initialize other subsystems if you
  120. * use them in your application.
  121. *
  122. * Logging (such as SDL_Log) works without initialization, too.
  123. *
  124. * `flags` may be any of the following OR'd together:
  125. *
  126. * - `SDL_INIT_AUDIO`: audio subsystem; automatically initializes the events
  127. * subsystem
  128. * - `SDL_INIT_VIDEO`: video subsystem; automatically initializes the events
  129. * subsystem
  130. * - `SDL_INIT_JOYSTICK`: joystick subsystem; automatically initializes the
  131. * events subsystem
  132. * - `SDL_INIT_HAPTIC`: haptic (force feedback) subsystem
  133. * - `SDL_INIT_GAMEPAD`: gamepad subsystem; automatically initializes the
  134. * joystick subsystem
  135. * - `SDL_INIT_EVENTS`: events subsystem
  136. * - `SDL_INIT_SENSOR`: sensor subsystem; automatically initializes the events
  137. * subsystem
  138. * - `SDL_INIT_CAMERA`: camera subsystem; automatically initializes the events
  139. * subsystem
  140. *
  141. * Subsystem initialization is ref-counted, you must call SDL_QuitSubSystem()
  142. * for each SDL_InitSubSystem() to correctly shutdown a subsystem manually (or
  143. * call SDL_Quit() to force shutdown). If a subsystem is already loaded then
  144. * this call will increase the ref-count and return.
  145. *
  146. * Consider reporting some basic metadata about your application before
  147. * calling SDL_Init, using either SDL_SetAppMetadata() or
  148. * SDL_SetAppMetadataProperty().
  149. *
  150. * \param flags subsystem initialization flags.
  151. * \returns true on success or false on failure; call SDL_GetError() for more
  152. * information.
  153. *
  154. * \since This function is available since SDL 3.0.0.
  155. *
  156. * \sa SDL_SetAppMetadata
  157. * \sa SDL_SetAppMetadataProperty
  158. * \sa SDL_InitSubSystem
  159. * \sa SDL_Quit
  160. * \sa SDL_SetMainReady
  161. * \sa SDL_WasInit
  162. */
  163. extern SDL_DECLSPEC bool SDLCALL SDL_Init(SDL_InitFlags flags);
  164. /**
  165. * Compatibility function to initialize the SDL library.
  166. *
  167. * This function and SDL_Init() are interchangeable.
  168. *
  169. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  170. * \returns true on success or false on failure; call SDL_GetError() for more
  171. * information.
  172. *
  173. * \since This function is available since SDL 3.0.0.
  174. *
  175. * \sa SDL_Init
  176. * \sa SDL_Quit
  177. * \sa SDL_QuitSubSystem
  178. */
  179. extern SDL_DECLSPEC bool SDLCALL SDL_InitSubSystem(SDL_InitFlags flags);
  180. /**
  181. * Shut down specific SDL subsystems.
  182. *
  183. * You still need to call SDL_Quit() even if you close all open subsystems
  184. * with SDL_QuitSubSystem().
  185. *
  186. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  187. *
  188. * \since This function is available since SDL 3.0.0.
  189. *
  190. * \sa SDL_InitSubSystem
  191. * \sa SDL_Quit
  192. */
  193. extern SDL_DECLSPEC void SDLCALL SDL_QuitSubSystem(SDL_InitFlags flags);
  194. /**
  195. * Get a mask of the specified subsystems which are currently initialized.
  196. *
  197. * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
  198. * \returns a mask of all initialized subsystems if `flags` is 0, otherwise it
  199. * returns the initialization status of the specified subsystems.
  200. *
  201. * \since This function is available since SDL 3.0.0.
  202. *
  203. * \sa SDL_Init
  204. * \sa SDL_InitSubSystem
  205. */
  206. extern SDL_DECLSPEC SDL_InitFlags SDLCALL SDL_WasInit(SDL_InitFlags flags);
  207. /**
  208. * Clean up all initialized subsystems.
  209. *
  210. * You should call this function even if you have already shutdown each
  211. * initialized subsystem with SDL_QuitSubSystem(). It is safe to call this
  212. * function even in the case of errors in initialization.
  213. *
  214. * You can use this function with atexit() to ensure that it is run when your
  215. * application is shutdown, but it is not wise to do this from a library or
  216. * other dynamically loaded code.
  217. *
  218. * \since This function is available since SDL 3.0.0.
  219. *
  220. * \sa SDL_Init
  221. * \sa SDL_QuitSubSystem
  222. */
  223. extern SDL_DECLSPEC void SDLCALL SDL_Quit(void);
  224. /**
  225. * Specify basic metadata about your app.
  226. *
  227. * You can optionally provide metadata about your app to SDL. This is not
  228. * required, but strongly encouraged.
  229. *
  230. * There are several locations where SDL can make use of metadata (an "About"
  231. * box in the macOS menu bar, the name of the app can be shown on some audio
  232. * mixers, etc). Any piece of metadata can be left as NULL, if a specific
  233. * detail doesn't make sense for the app.
  234. *
  235. * This function should be called as early as possible, before SDL_Init.
  236. * Multiple calls to this function are allowed, but various state might not
  237. * change once it has been set up with a previous call to this function.
  238. *
  239. * Passing a NULL removes any previous metadata.
  240. *
  241. * This is a simplified interface for the most important information. You can
  242. * supply significantly more detailed metadata with
  243. * SDL_SetAppMetadataProperty().
  244. *
  245. * \param appname The name of the application ("My Game 2: Bad Guy's
  246. * Revenge!").
  247. * \param appversion The version of the application ("1.0.0beta5" or a git
  248. * hash, or whatever makes sense).
  249. * \param appidentifier A unique string in reverse-domain format that
  250. * identifies this app ("com.example.mygame2").
  251. * \returns true on success or false on failure; call SDL_GetError() for more
  252. * information.
  253. *
  254. * \threadsafety It is safe to call this function from any thread.
  255. *
  256. * \since This function is available since SDL 3.0.0.
  257. *
  258. * \sa SDL_SetAppMetadataProperty
  259. */
  260. extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier);
  261. /**
  262. * Specify metadata about your app through a set of properties.
  263. *
  264. * You can optionally provide metadata about your app to SDL. This is not
  265. * required, but strongly encouraged.
  266. *
  267. * There are several locations where SDL can make use of metadata (an "About"
  268. * box in the macOS menu bar, the name of the app can be shown on some audio
  269. * mixers, etc). Any piece of metadata can be left out, if a specific detail
  270. * doesn't make sense for the app.
  271. *
  272. * This function should be called as early as possible, before SDL_Init.
  273. * Multiple calls to this function are allowed, but various state might not
  274. * change once it has been set up with a previous call to this function.
  275. *
  276. * Once set, this metadata can be read using SDL_GetMetadataProperty().
  277. *
  278. * These are the supported properties:
  279. *
  280. * - `SDL_PROP_APP_METADATA_NAME_STRING`: The human-readable name of the
  281. * application, like "My Game 2: Bad Guy's Revenge!". This will show up
  282. * anywhere the OS shows the name of the application separately from window
  283. * titles, such as volume control applets, etc. This defaults to "SDL
  284. * Application".
  285. * - `SDL_PROP_APP_METADATA_VERSION_STRING`: The version of the app that is
  286. * running; there are no rules on format, so "1.0.3beta2" and "April 22nd,
  287. * 2024" and a git hash are all valid options. This has no default.
  288. * - `SDL_PROP_APP_METADATA_IDENTIFIER_STRING`: A unique string that
  289. * identifies this app. This must be in reverse-domain format, like
  290. * "com.example.mygame2". This string is used by desktop compositors to
  291. * identify and group windows together, as well as match applications with
  292. * associated desktop settings and icons. If you plan to package your
  293. * application in a container such as Flatpak, the app ID should match the
  294. * name of your Flatpak container as well. This has no default.
  295. * - `SDL_PROP_APP_METADATA_CREATOR_STRING`: The human-readable name of the
  296. * creator/developer/maker of this app, like "MojoWorkshop, LLC"
  297. * - `SDL_PROP_APP_METADATA_COPYRIGHT_STRING`: The human-readable copyright
  298. * notice, like "Copyright (c) 2024 MojoWorkshop, LLC" or whatnot. Keep this
  299. * to one line, don't paste a copy of a whole software license in here. This
  300. * has no default.
  301. * - `SDL_PROP_APP_METADATA_URL_STRING`: A URL to the app on the web. Maybe a
  302. * product page, or a storefront, or even a GitHub repository, for user's
  303. * further information This has no default.
  304. * - `SDL_PROP_APP_METADATA_TYPE_STRING`: The type of application this is.
  305. * Currently this string can be "game" for a video game, "mediaplayer" for a
  306. * media player, or generically "application" if nothing else applies.
  307. * Future versions of SDL might add new types. This defaults to
  308. * "application".
  309. *
  310. * \param name the name of the metadata property to set.
  311. * \param value the value of the property, or NULL to remove that property.
  312. * \returns true on success or false on failure; call SDL_GetError() for more
  313. * information.
  314. *
  315. * \threadsafety It is safe to call this function from any thread.
  316. *
  317. * \since This function is available since SDL 3.0.0.
  318. *
  319. * \sa SDL_GetAppMetadataProperty
  320. * \sa SDL_SetAppMetadata
  321. */
  322. extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadataProperty(const char *name, const char *value);
  323. #define SDL_PROP_APP_METADATA_NAME_STRING "SDL.app.metadata.name"
  324. #define SDL_PROP_APP_METADATA_VERSION_STRING "SDL.app.metadata.version"
  325. #define SDL_PROP_APP_METADATA_IDENTIFIER_STRING "SDL.app.metadata.identifier"
  326. #define SDL_PROP_APP_METADATA_CREATOR_STRING "SDL.app.metadata.creator"
  327. #define SDL_PROP_APP_METADATA_COPYRIGHT_STRING "SDL.app.metadata.copyright"
  328. #define SDL_PROP_APP_METADATA_URL_STRING "SDL.app.metadata.url"
  329. #define SDL_PROP_APP_METADATA_TYPE_STRING "SDL.app.metadata.type"
  330. /**
  331. * Get metadata about your app.
  332. *
  333. * This returns metadata previously set using SDL_SetAppMetadata() or
  334. * SDL_SetAppMetadataProperty(). See SDL_SetAppMetadataProperty() for the list
  335. * of available properties and their meanings.
  336. *
  337. * \param name the name of the metadata property to get.
  338. * \returns the current value of the metadata property, or the default if it
  339. * is not set, NULL for properties with no default.
  340. *
  341. * \threadsafety It is safe to call this function from any thread, although
  342. * the string returned is not protected and could potentially be
  343. * freed if you call SDL_SetAppMetadataProperty() to set that
  344. * property from another thread.
  345. *
  346. * \since This function is available since SDL 3.0.0.
  347. *
  348. * \sa SDL_SetAppMetadata
  349. * \sa SDL_SetAppMetadataProperty
  350. */
  351. extern SDL_DECLSPEC const char * SDLCALL SDL_GetAppMetadataProperty(const char *name);
  352. /* Ends C function definitions when using C++ */
  353. #ifdef __cplusplus
  354. }
  355. #endif
  356. #include <SDL3/SDL_close_code.h>
  357. #endif /* SDL_init_h_ */