SDL 3.0
SDL_init.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryInit
24 *
25 * SDL subsystem init and quit functions.
26 */
27
28
29#ifndef SDL_init_h_
30#define SDL_init_h_
31
32#include <SDL3/SDL_stdinc.h>
33#include <SDL3/SDL_error.h>
34#include <SDL3/SDL_events.h>
35
36#include <SDL3/SDL_begin_code.h>
37/* Set up for C function definitions, even when using C++ */
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/* As of version 0.5, SDL is loaded dynamically into the application */
43
44/**
45 * Initialization flags for SDL_Init and/or SDL_InitSubSystem
46 *
47 * These are the flags which may be passed to SDL_Init(). You should specify
48 * the subsystems which you will be using in your application.
49 *
50 * \since This datatype is available since SDL 3.0.0.
51 *
52 * \sa SDL_Init
53 * \sa SDL_Quit
54 * \sa SDL_InitSubSystem
55 * \sa SDL_QuitSubSystem
56 * \sa SDL_WasInit
57 */
59
60#define SDL_INIT_AUDIO 0x00000010u /**< `SDL_INIT_AUDIO` implies `SDL_INIT_EVENTS` */
61#define SDL_INIT_VIDEO 0x00000020u /**< `SDL_INIT_VIDEO` implies `SDL_INIT_EVENTS` */
62#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 */
63#define SDL_INIT_HAPTIC 0x00001000u
64#define SDL_INIT_GAMEPAD 0x00002000u /**< `SDL_INIT_GAMEPAD` implies `SDL_INIT_JOYSTICK` */
65#define SDL_INIT_EVENTS 0x00004000u
66#define SDL_INIT_SENSOR 0x00008000u /**< `SDL_INIT_SENSOR` implies `SDL_INIT_EVENTS` */
67#define SDL_INIT_CAMERA 0x00010000u /**< `SDL_INIT_CAMERA` implies `SDL_INIT_EVENTS` */
68
69/**
70 * Return values for optional main callbacks.
71 *
72 * Returning SDL_APP_SUCCESS or SDL_APP_FAILURE from SDL_AppInit,
73 * SDL_AppEvent, or SDL_AppIterate will terminate the program and report
74 * success/failure to the operating system. What that means is
75 * platform-dependent. On Unix, for example, on success, the process error
76 * code will be zero, and on failure it will be 1. This interface doesn't
77 * allow you to return specific exit codes, just whether there was an error
78 * generally or not.
79 *
80 * Returning SDL_APP_CONTINUE from these functions will let the app continue
81 * to run.
82 *
83 * See
84 * [Main callbacks in SDL3](https://wiki.libsdl.org/SDL3/README/main-functions#main-callbacks-in-sdl3)
85 * for complete details.
86 *
87 * \since This enum is available since SDL 3.0.0.
88 */
89typedef enum SDL_AppResult
90{
91 SDL_APP_CONTINUE, /**< Value that requests that the app continue from the main callbacks. */
92 SDL_APP_SUCCESS, /**< Value that requests termination with success from the main callbacks. */
93 SDL_APP_FAILURE /**< Value that requests termination with error from the main callbacks. */
95
96typedef SDL_AppResult (SDLCALL *SDL_AppInit_func)(void **appstate, int argc, char *argv[]);
97typedef SDL_AppResult (SDLCALL *SDL_AppIterate_func)(void *appstate);
98typedef SDL_AppResult (SDLCALL *SDL_AppEvent_func)(void *appstate, SDL_Event *event);
99typedef void (SDLCALL *SDL_AppQuit_func)(void *appstate, SDL_AppResult result);
100
101/**
102 * Initialize the SDL library.
103 *
104 * SDL_Init() simply forwards to calling SDL_InitSubSystem(). Therefore, the
105 * two may be used interchangeably. Though for readability of your code
106 * SDL_InitSubSystem() might be preferred.
107 *
108 * The file I/O (for example: SDL_IOFromFile) and threading (SDL_CreateThread)
109 * subsystems are initialized by default. Message boxes
110 * (SDL_ShowSimpleMessageBox) also attempt to work without initializing the
111 * video subsystem, in hopes of being useful in showing an error dialog when
112 * SDL_Init fails. You must specifically initialize other subsystems if you
113 * use them in your application.
114 *
115 * Logging (such as SDL_Log) works without initialization, too.
116 *
117 * `flags` may be any of the following OR'd together:
118 *
119 * - `SDL_INIT_AUDIO`: audio subsystem; automatically initializes the events
120 * subsystem
121 * - `SDL_INIT_VIDEO`: video subsystem; automatically initializes the events
122 * subsystem
123 * - `SDL_INIT_JOYSTICK`: joystick subsystem; automatically initializes the
124 * events subsystem
125 * - `SDL_INIT_HAPTIC`: haptic (force feedback) subsystem
126 * - `SDL_INIT_GAMEPAD`: gamepad subsystem; automatically initializes the
127 * joystick subsystem
128 * - `SDL_INIT_EVENTS`: events subsystem
129 * - `SDL_INIT_SENSOR`: sensor subsystem; automatically initializes the events
130 * subsystem
131 * - `SDL_INIT_CAMERA`: camera subsystem; automatically initializes the events
132 * subsystem
133 *
134 * Subsystem initialization is ref-counted, you must call SDL_QuitSubSystem()
135 * for each SDL_InitSubSystem() to correctly shutdown a subsystem manually (or
136 * call SDL_Quit() to force shutdown). If a subsystem is already loaded then
137 * this call will increase the ref-count and return.
138 *
139 * Consider reporting some basic metadata about your application before
140 * calling SDL_Init, using either SDL_SetAppMetadata() or
141 * SDL_SetAppMetadataProperty().
142 *
143 * \param flags subsystem initialization flags.
144 * \returns true on success or false on failure; call SDL_GetError() for more
145 * information.
146 *
147 * \since This function is available since SDL 3.0.0.
148 *
149 * \sa SDL_SetAppMetadata
150 * \sa SDL_SetAppMetadataProperty
151 * \sa SDL_InitSubSystem
152 * \sa SDL_Quit
153 * \sa SDL_SetMainReady
154 * \sa SDL_WasInit
155 */
156extern SDL_DECLSPEC bool SDLCALL SDL_Init(SDL_InitFlags flags);
157
158/**
159 * Compatibility function to initialize the SDL library.
160 *
161 * This function and SDL_Init() are interchangeable.
162 *
163 * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
164 * \returns true on success or false on failure; call SDL_GetError() for more
165 * information.
166 *
167 * \since This function is available since SDL 3.0.0.
168 *
169 * \sa SDL_Init
170 * \sa SDL_Quit
171 * \sa SDL_QuitSubSystem
172 */
173extern SDL_DECLSPEC bool SDLCALL SDL_InitSubSystem(SDL_InitFlags flags);
174
175/**
176 * Shut down specific SDL subsystems.
177 *
178 * You still need to call SDL_Quit() even if you close all open subsystems
179 * with SDL_QuitSubSystem().
180 *
181 * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
182 *
183 * \since This function is available since SDL 3.0.0.
184 *
185 * \sa SDL_InitSubSystem
186 * \sa SDL_Quit
187 */
188extern SDL_DECLSPEC void SDLCALL SDL_QuitSubSystem(SDL_InitFlags flags);
189
190/**
191 * Get a mask of the specified subsystems which are currently initialized.
192 *
193 * \param flags any of the flags used by SDL_Init(); see SDL_Init for details.
194 * \returns a mask of all initialized subsystems if `flags` is 0, otherwise it
195 * returns the initialization status of the specified subsystems.
196 *
197 * \since This function is available since SDL 3.0.0.
198 *
199 * \sa SDL_Init
200 * \sa SDL_InitSubSystem
201 */
202extern SDL_DECLSPEC SDL_InitFlags SDLCALL SDL_WasInit(SDL_InitFlags flags);
203
204/**
205 * Clean up all initialized subsystems.
206 *
207 * You should call this function even if you have already shutdown each
208 * initialized subsystem with SDL_QuitSubSystem(). It is safe to call this
209 * function even in the case of errors in initialization.
210 *
211 * You can use this function with atexit() to ensure that it is run when your
212 * application is shutdown, but it is not wise to do this from a library or
213 * other dynamically loaded code.
214 *
215 * \since This function is available since SDL 3.0.0.
216 *
217 * \sa SDL_Init
218 * \sa SDL_QuitSubSystem
219 */
220extern SDL_DECLSPEC void SDLCALL SDL_Quit(void);
221
222/**
223 * Specify basic metadata about your app.
224 *
225 * You can optionally provide metadata about your app to SDL. This is not
226 * required, but strongly encouraged.
227 *
228 * There are several locations where SDL can make use of metadata (an "About"
229 * box in the macOS menu bar, the name of the app can be shown on some audio
230 * mixers, etc). Any piece of metadata can be left as NULL, if a specific
231 * detail doesn't make sense for the app.
232 *
233 * This function should be called as early as possible, before SDL_Init.
234 * Multiple calls to this function are allowed, but various state might not
235 * change once it has been set up with a previous call to this function.
236 *
237 * Passing a NULL removes any previous metadata.
238 *
239 * This is a simplified interface for the most important information. You can
240 * supply significantly more detailed metadata with
241 * SDL_SetAppMetadataProperty().
242 *
243 * \param appname The name of the application ("My Game 2: Bad Guy's
244 * Revenge!").
245 * \param appversion The version of the application ("1.0.0beta5" or a git
246 * hash, or whatever makes sense).
247 * \param appidentifier A unique string in reverse-domain format that
248 * identifies this app ("com.example.mygame2").
249 * \returns true on success or false on failure; call SDL_GetError() for more
250 * information.
251 *
252 * \threadsafety It is safe to call this function from any thread.
253 *
254 * \since This function is available since SDL 3.0.0.
255 *
256 * \sa SDL_SetAppMetadataProperty
257 */
258extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier);
259
260/**
261 * Specify metadata about your app through a set of properties.
262 *
263 * You can optionally provide metadata about your app to SDL. This is not
264 * required, but strongly encouraged.
265 *
266 * There are several locations where SDL can make use of metadata (an "About"
267 * box in the macOS menu bar, the name of the app can be shown on some audio
268 * mixers, etc). Any piece of metadata can be left out, if a specific detail
269 * doesn't make sense for the app.
270 *
271 * This function should be called as early as possible, before SDL_Init.
272 * Multiple calls to this function are allowed, but various state might not
273 * change once it has been set up with a previous call to this function.
274 *
275 * Once set, this metadata can be read using SDL_GetMetadataProperty().
276 *
277 * These are the supported properties:
278 *
279 * - `SDL_PROP_APP_METADATA_NAME_STRING`: The human-readable name of the
280 * application, like "My Game 2: Bad Guy's Revenge!". This will show up
281 * anywhere the OS shows the name of the application separately from window
282 * titles, such as volume control applets, etc. This defaults to "SDL
283 * Application".
284 * - `SDL_PROP_APP_METADATA_VERSION_STRING`: The version of the app that is
285 * running; there are no rules on format, so "1.0.3beta2" and "April 22nd,
286 * 2024" and a git hash are all valid options. This has no default.
287 * - `SDL_PROP_APP_METADATA_IDENTIFIER_STRING`: A unique string that
288 * identifies this app. This must be in reverse-domain format, like
289 * "com.example.mygame2". This string is used by desktop compositors to
290 * identify and group windows together, as well as match applications with
291 * associated desktop settings and icons. If you plan to package your
292 * application in a container such as Flatpak, the app ID should match the
293 * name of your Flatpak container as well. This has no default.
294 * - `SDL_PROP_APP_METADATA_CREATOR_STRING`: The human-readable name of the
295 * creator/developer/maker of this app, like "MojoWorkshop, LLC"
296 * - `SDL_PROP_APP_METADATA_COPYRIGHT_STRING`: The human-readable copyright
297 * notice, like "Copyright (c) 2024 MojoWorkshop, LLC" or whatnot. Keep this
298 * to one line, don't paste a copy of a whole software license in here. This
299 * has no default.
300 * - `SDL_PROP_APP_METADATA_URL_STRING`: A URL to the app on the web. Maybe a
301 * product page, or a storefront, or even a GitHub repository, for user's
302 * further information This has no default.
303 * - `SDL_PROP_APP_METADATA_TYPE_STRING`: The type of application this is.
304 * Currently this string can be "game" for a video game, "mediaplayer" for a
305 * media player, or generically "application" if nothing else applies.
306 * Future versions of SDL might add new types. This defaults to
307 * "application".
308 *
309 * \param name the name of the metadata property to set.
310 * \param value the value of the property, or NULL to remove that property.
311 * \returns true on success or false on failure; call SDL_GetError() for more
312 * information.
313 *
314 * \threadsafety It is safe to call this function from any thread.
315 *
316 * \since This function is available since SDL 3.0.0.
317 *
318 * \sa SDL_GetAppMetadataProperty
319 * \sa SDL_SetAppMetadata
320 */
321extern SDL_DECLSPEC bool SDLCALL SDL_SetAppMetadataProperty(const char *name, const char *value);
322
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/**
332 * Get metadata about your app.
333 *
334 * This returns metadata previously set using SDL_SetAppMetadata() or
335 * SDL_SetAppMetadataProperty(). See SDL_SetAppMetadataProperty() for the list
336 * of available properties and their meanings.
337 *
338 * \param name the name of the metadata property to get.
339 * \returns the current value of the metadata property, or the default if it
340 * is not set, NULL for properties with no default.
341 *
342 * \threadsafety It is safe to call this function from any thread, although
343 * the string returned is not protected and could potentially be
344 * freed if you call SDL_SetAppMetadataProperty() to set that
345 * property from another thread.
346 *
347 * \since This function is available since SDL 3.0.0.
348 *
349 * \sa SDL_SetAppMetadata
350 * \sa SDL_SetAppMetadataProperty
351 */
352extern SDL_DECLSPEC const char * SDLCALL SDL_GetAppMetadataProperty(const char *name);
353
354/* Ends C function definitions when using C++ */
355#ifdef __cplusplus
356}
357#endif
358#include <SDL3/SDL_close_code.h>
359
360#endif /* SDL_init_h_ */
SDL_AppResult(* SDL_AppEvent_func)(void *appstate, SDL_Event *event)
Definition SDL_init.h:98
Uint32 SDL_InitFlags
Definition SDL_init.h:58
bool SDL_SetAppMetadataProperty(const char *name, const char *value)
void SDL_QuitSubSystem(SDL_InitFlags flags)
SDL_InitFlags SDL_WasInit(SDL_InitFlags flags)
SDL_AppResult(* SDL_AppInit_func)(void **appstate, int argc, char *argv[])
Definition SDL_init.h:96
const char * SDL_GetAppMetadataProperty(const char *name)
bool SDL_InitSubSystem(SDL_InitFlags flags)
void SDL_Quit(void)
SDL_AppResult
Definition SDL_init.h:90
@ SDL_APP_CONTINUE
Definition SDL_init.h:91
@ SDL_APP_SUCCESS
Definition SDL_init.h:92
@ SDL_APP_FAILURE
Definition SDL_init.h:93
bool SDL_Init(SDL_InitFlags flags)
bool SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier)
void(* SDL_AppQuit_func)(void *appstate, SDL_AppResult result)
Definition SDL_init.h:99
SDL_AppResult(* SDL_AppIterate_func)(void *appstate)
Definition SDL_init.h:97
uint32_t Uint32
Definition SDL_stdinc.h:356