sdl3_sys/generated/main.rs
1//! Redefine main() if necessary so that it is called by SDL.
2//!
3//! In order to make this consistent on all platforms, the application's main()
4//! should look like this:
5//!
6//! ```c
7//! #include <SDL3/SDL.h>
8//! #include <SDL3/SDL_main.h>
9//!
10//! int main(int argc, char *argv[])
11//! {
12//! }
13//! ```
14//!
15//! SDL will take care of platform specific details on how it gets called.
16//!
17//! This is also where an app can be configured to use the main callbacks, via
18//! the SDL_MAIN_USE_CALLBACKS macro.
19//!
20//! SDL_main.h is a "single-header library," which is to say that including
21//! this header inserts code into your program, and you should only include it
22//! once in most cases. SDL.h does not include this header automatically.
23//!
24//! For more information, see:
25//!
26//! <https://wiki.libsdl.org/SDL3/README-main-functions>
27
28use super::stdinc::*;
29
30use super::error::*;
31
32use super::events::*;
33
34apply_cfg!(#[cfg(doc)] => {
35 /// Inform SDL that the app is providing an entry point instead of SDL.
36 ///
37 /// SDL does not define this macro, but will check if it is defined when
38 /// including `SDL_main.h`. If defined, SDL will expect the app to provide the
39 /// proper entry point for the platform, and all the other magic details
40 /// needed, like manually calling [`SDL_SetMainReady`].
41 ///
42 /// Please see [README-main-functions](README-main-functions), (or
43 /// docs/README-main-functions.md in the source tree) for a more detailed
44 /// explanation.
45 ///
46 /// ## Availability
47 /// This macro is used by the headers since SDL 3.2.0.
48 pub const SDL_MAIN_HANDLED: ::core::primitive::i32 = 1;
49
50});
51
52apply_cfg!(#[cfg(doc)] => {
53});
54
55apply_cfg!(#[cfg(not(doc))] => {
56});
57
58use super::init::*;
59
60unsafe extern "C" {
61 /// App-implemented initial entry point for SDL_MAIN_USE_CALLBACKS apps.
62 ///
63 /// Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If using a
64 /// standard "main" function, you should not supply this.
65 ///
66 /// This function is called by SDL once, at startup. The function should
67 /// initialize whatever is necessary, possibly create windows and open audio
68 /// devices, etc. The `argc` and `argv` parameters work like they would with a
69 /// standard "main" function.
70 ///
71 /// This function should not go into an infinite mainloop; it should do any
72 /// one-time setup it requires and then return.
73 ///
74 /// The app may optionally assign a pointer to `*appstate`. This pointer will
75 /// be provided on every future call to the other entry points, to allow
76 /// application state to be preserved between functions without the app needing
77 /// to use a global variable. If this isn't set, the pointer will be NULL in
78 /// future entry points.
79 ///
80 /// If this function returns [`SDL_APP_CONTINUE`], the app will proceed to normal
81 /// operation, and will begin receiving repeated calls to [`SDL_AppIterate`] and
82 /// [`SDL_AppEvent`] for the life of the program. If this function returns
83 /// [`SDL_APP_FAILURE`], SDL will call [`SDL_AppQuit`] and terminate the process with
84 /// an exit code that reports an error to the platform. If it returns
85 /// [`SDL_APP_SUCCESS`], SDL calls [`SDL_AppQuit`] and terminates with an exit code
86 /// that reports success to the platform.
87 ///
88 /// This function is called by SDL on the main thread.
89 ///
90 /// ## Parameters
91 /// - `appstate`: a place where the app can optionally store a pointer for
92 /// future use.
93 /// - `argc`: the standard ANSI C main's argc; number of elements in `argv`.
94 /// - `argv`: the standard ANSI C main's argv; array of command line
95 /// arguments.
96 ///
97 /// ## Return value
98 /// Returns [`SDL_APP_FAILURE`] to terminate with an error, [`SDL_APP_SUCCESS`] to
99 /// terminate with success, [`SDL_APP_CONTINUE`] to continue.
100 ///
101 /// ## Availability
102 /// This function is available since SDL 3.2.0.
103 ///
104 /// ## See also
105 /// - [`SDL_AppIterate`]
106 /// - [`SDL_AppEvent`]
107 /// - [`SDL_AppQuit`]
108 pub fn SDL_AppInit(
109 appstate: *mut *mut ::core::ffi::c_void,
110 argc: ::core::ffi::c_int,
111 argv: *mut *mut ::core::ffi::c_char,
112 ) -> SDL_AppResult;
113}
114
115unsafe extern "C" {
116 /// App-implemented iteration entry point for SDL_MAIN_USE_CALLBACKS apps.
117 ///
118 /// Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If using a
119 /// standard "main" function, you should not supply this.
120 ///
121 /// This function is called repeatedly by SDL after [`SDL_AppInit`] returns
122 /// [`SDL_APP_CONTINUE`]. The function should operate as a single iteration the
123 /// program's primary loop; it should update whatever state it needs and draw a
124 /// new frame of video, usually.
125 ///
126 /// On some platforms, this function will be called at the refresh rate of the
127 /// display (which might change during the life of your app!). There are no
128 /// promises made about what frequency this function might run at. You should
129 /// use SDL's timer functions if you need to see how much time has passed since
130 /// the last iteration.
131 ///
132 /// There is no need to process the SDL event queue during this function; SDL
133 /// will send events as they arrive in [`SDL_AppEvent`], and in most cases the
134 /// event queue will be empty when this function runs anyhow.
135 ///
136 /// This function should not go into an infinite mainloop; it should do one
137 /// iteration of whatever the program does and return.
138 ///
139 /// The `appstate` parameter is an optional pointer provided by the app during
140 /// [`SDL_AppInit()`]. If the app never provided a pointer, this will be NULL.
141 ///
142 /// If this function returns [`SDL_APP_CONTINUE`], the app will continue normal
143 /// operation, receiving repeated calls to [`SDL_AppIterate`] and [`SDL_AppEvent`] for
144 /// the life of the program. If this function returns [`SDL_APP_FAILURE`], SDL will
145 /// call [`SDL_AppQuit`] and terminate the process with an exit code that reports
146 /// an error to the platform. If it returns [`SDL_APP_SUCCESS`], SDL calls
147 /// [`SDL_AppQuit`] and terminates with an exit code that reports success to the
148 /// platform.
149 ///
150 /// This function is called by SDL on the main thread.
151 ///
152 /// ## Parameters
153 /// - `appstate`: an optional pointer, provided by the app in [`SDL_AppInit`].
154 ///
155 /// ## Return value
156 /// Returns [`SDL_APP_FAILURE`] to terminate with an error, [`SDL_APP_SUCCESS`] to
157 /// terminate with success, [`SDL_APP_CONTINUE`] to continue.
158 ///
159 /// ## Thread safety
160 /// This function may get called concurrently with [`SDL_AppEvent()`]
161 /// for events not pushed on the main thread.
162 ///
163 /// ## Availability
164 /// This function is available since SDL 3.2.0.
165 ///
166 /// ## See also
167 /// - [`SDL_AppInit`]
168 /// - [`SDL_AppEvent`]
169 pub fn SDL_AppIterate(appstate: *mut ::core::ffi::c_void) -> SDL_AppResult;
170}
171
172unsafe extern "C" {
173 /// App-implemented event entry point for SDL_MAIN_USE_CALLBACKS apps.
174 ///
175 /// Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If using a
176 /// standard "main" function, you should not supply this.
177 ///
178 /// This function is called as needed by SDL after [`SDL_AppInit`] returns
179 /// [`SDL_APP_CONTINUE`]. It is called once for each new event.
180 ///
181 /// There is (currently) no guarantee about what thread this will be called
182 /// from; whatever thread pushes an event onto SDL's queue will trigger this
183 /// function. SDL is responsible for pumping the event queue between each call
184 /// to [`SDL_AppIterate`], so in normal operation one should only get events in a
185 /// serial fashion, but be careful if you have a thread that explicitly calls
186 /// [`SDL_PushEvent`]. SDL itself will push events to the queue on the main thread.
187 ///
188 /// Events sent to this function are not owned by the app; if you need to save
189 /// the data, you should copy it.
190 ///
191 /// This function should not go into an infinite mainloop; it should handle the
192 /// provided event appropriately and return.
193 ///
194 /// The `appstate` parameter is an optional pointer provided by the app during
195 /// [`SDL_AppInit()`]. If the app never provided a pointer, this will be NULL.
196 ///
197 /// If this function returns [`SDL_APP_CONTINUE`], the app will continue normal
198 /// operation, receiving repeated calls to [`SDL_AppIterate`] and [`SDL_AppEvent`] for
199 /// the life of the program. If this function returns [`SDL_APP_FAILURE`], SDL will
200 /// call [`SDL_AppQuit`] and terminate the process with an exit code that reports
201 /// an error to the platform. If it returns [`SDL_APP_SUCCESS`], SDL calls
202 /// [`SDL_AppQuit`] and terminates with an exit code that reports success to the
203 /// platform.
204 ///
205 /// ## Parameters
206 /// - `appstate`: an optional pointer, provided by the app in [`SDL_AppInit`].
207 /// - `event`: the new event for the app to examine.
208 ///
209 /// ## Return value
210 /// Returns [`SDL_APP_FAILURE`] to terminate with an error, [`SDL_APP_SUCCESS`] to
211 /// terminate with success, [`SDL_APP_CONTINUE`] to continue.
212 ///
213 /// ## Thread safety
214 /// This function may get called concurrently with
215 /// [`SDL_AppIterate()`] or [`SDL_AppQuit()`] for events not pushed from
216 /// the main thread.
217 ///
218 /// ## Availability
219 /// This function is available since SDL 3.2.0.
220 ///
221 /// ## See also
222 /// - [`SDL_AppInit`]
223 /// - [`SDL_AppIterate`]
224 pub fn SDL_AppEvent(appstate: *mut ::core::ffi::c_void, event: *mut SDL_Event)
225 -> SDL_AppResult;
226}
227
228unsafe extern "C" {
229 /// App-implemented deinit entry point for SDL_MAIN_USE_CALLBACKS apps.
230 ///
231 /// Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If using a
232 /// standard "main" function, you should not supply this.
233 ///
234 /// This function is called once by SDL before terminating the program.
235 ///
236 /// This function will be called in all cases, even if [`SDL_AppInit`] requests
237 /// termination at startup.
238 ///
239 /// This function should not go into an infinite mainloop; it should
240 /// deinitialize any resources necessary, perform whatever shutdown activities,
241 /// and return.
242 ///
243 /// You do not need to call [`SDL_Quit()`] in this function, as SDL will call it
244 /// after this function returns and before the process terminates, but it is
245 /// safe to do so.
246 ///
247 /// The `appstate` parameter is an optional pointer provided by the app during
248 /// [`SDL_AppInit()`]. If the app never provided a pointer, this will be NULL. This
249 /// function call is the last time this pointer will be provided, so any
250 /// resources to it should be cleaned up here.
251 ///
252 /// This function is called by SDL on the main thread.
253 ///
254 /// ## Parameters
255 /// - `appstate`: an optional pointer, provided by the app in [`SDL_AppInit`].
256 /// - `result`: the result code that terminated the app (success or failure).
257 ///
258 /// ## Thread safety
259 /// [`SDL_AppEvent()`] may get called concurrently with this function
260 /// if other threads that push events are still active.
261 ///
262 /// ## Availability
263 /// This function is available since SDL 3.2.0.
264 ///
265 /// ## See also
266 /// - [`SDL_AppInit`]
267 pub fn SDL_AppQuit(appstate: *mut ::core::ffi::c_void, result: SDL_AppResult);
268}
269
270/// The prototype for the application's main() function
271///
272/// ## Parameters
273/// - `argc`: an ANSI-C style main function's argc.
274/// - `argv`: an ANSI-C style main function's argv.
275///
276/// ## Return value
277/// Returns an ANSI-C main return code; generally 0 is considered successful
278/// program completion, and small non-zero values are considered
279/// errors.
280///
281/// ## Availability
282/// This datatype is available since SDL 3.2.0.
283pub type SDL_main_func = ::core::option::Option<
284 unsafe extern "C" fn(
285 argc: ::core::ffi::c_int,
286 argv: *mut *mut ::core::ffi::c_char,
287 ) -> ::core::ffi::c_int,
288>;
289
290unsafe extern "C" {
291 /// An app-supplied function for program entry.
292 ///
293 /// Apps do not directly create this function; they should create a standard
294 /// ANSI-C `main` function instead. If SDL needs to insert some startup code
295 /// before `main` runs, or the platform doesn't actually _use_ a function
296 /// called "main", SDL will do some macro magic to redefine `main` to
297 /// [`SDL_main`] and provide its own `main`.
298 ///
299 /// Apps should include `SDL_main.h` in the same file as their `main` function,
300 /// and they should not use that symbol for anything else in that file, as it
301 /// might get redefined.
302 ///
303 /// This function is only provided by the app if it isn't using
304 /// SDL_MAIN_USE_CALLBACKS.
305 ///
306 /// Program startup is a surprisingly complex topic. Please see
307 /// [README-main-functions](README-main-functions), (or
308 /// docs/README-main-functions.md in the source tree) for a more detailed
309 /// explanation.
310 ///
311 /// ## Parameters
312 /// - `argc`: an ANSI-C style main function's argc.
313 /// - `argv`: an ANSI-C style main function's argv.
314 ///
315 /// ## Return value
316 /// Returns an ANSI-C main return code; generally 0 is considered successful
317 /// program completion, and small non-zero values are considered
318 /// errors.
319 ///
320 /// ## Thread safety
321 /// This is the program entry point.
322 ///
323 /// ## Availability
324 /// This function is available since SDL 3.2.0.
325 pub fn SDL_main(
326 argc: ::core::ffi::c_int,
327 argv: *mut *mut ::core::ffi::c_char,
328 ) -> ::core::ffi::c_int;
329}
330
331unsafe extern "C" {
332 /// Circumvent failure of [`SDL_Init()`] when not using [`SDL_main()`] as an entry
333 /// point.
334 ///
335 /// This function is defined in SDL_main.h, along with the preprocessor rule to
336 /// redefine main() as [`SDL_main()`]. Thus to ensure that your main() function
337 /// will not be changed it is necessary to define SDL_MAIN_HANDLED before
338 /// including SDL.h.
339 ///
340 /// ## Availability
341 /// This function is available since SDL 3.2.0.
342 ///
343 /// ## See also
344 /// - [`SDL_Init`]
345 pub fn SDL_SetMainReady();
346}
347
348unsafe extern "C" {
349 /// Initializes and launches an SDL application, by doing platform-specific
350 /// initialization before calling your mainFunction and cleanups after it
351 /// returns, if that is needed for a specific platform, otherwise it just calls
352 /// mainFunction.
353 ///
354 /// You can use this if you want to use your own main() implementation without
355 /// using [`SDL_main`] (like when using SDL_MAIN_HANDLED). When using this, you do
356 /// *not* need [`SDL_SetMainReady()`].
357 ///
358 /// If `argv` is NULL, SDL will provide command line arguments, either by
359 /// querying the OS for them if possible, or supplying a filler array if not.
360 ///
361 /// ## Parameters
362 /// - `argc`: the argc parameter from the application's main() function, or 0
363 /// if the platform's main-equivalent has no argc.
364 /// - `argv`: the argv parameter from the application's main() function, or
365 /// NULL if the platform's main-equivalent has no argv.
366 /// - `mainFunction`: your SDL app's C-style main(). NOT the function you're
367 /// calling this from! Its name doesn't matter; it doesn't
368 /// literally have to be `main`.
369 /// - `reserved`: should be NULL (reserved for future use, will probably be
370 /// platform-specific then).
371 ///
372 /// ## Return value
373 /// Returns the return value from mainFunction: 0 on success, otherwise
374 /// failure; [`SDL_GetError()`] might have more information on the
375 /// failure.
376 ///
377 /// ## Thread safety
378 /// Generally this is called once, near startup, from the
379 /// process's initial thread.
380 ///
381 /// ## Availability
382 /// This function is available since SDL 3.2.0.
383 pub fn SDL_RunApp(
384 argc: ::core::ffi::c_int,
385 argv: *mut *mut ::core::ffi::c_char,
386 mainFunction: SDL_main_func,
387 reserved: *mut ::core::ffi::c_void,
388 ) -> ::core::ffi::c_int;
389}
390
391unsafe extern "C" {
392 /// An entry point for SDL's use in SDL_MAIN_USE_CALLBACKS.
393 ///
394 /// Generally, you should not call this function directly. This only exists to
395 /// hand off work into SDL as soon as possible, where it has a lot more control
396 /// and functionality available, and make the inline code in SDL_main.h as
397 /// small as possible.
398 ///
399 /// Not all platforms use this, it's actual use is hidden in a magic
400 /// header-only library, and you should not call this directly unless you
401 /// _really_ know what you're doing.
402 ///
403 /// ## Parameters
404 /// - `argc`: standard Unix main argc.
405 /// - `argv`: standard Unix main argv.
406 /// - `appinit`: the application's [`SDL_AppInit`] function.
407 /// - `appiter`: the application's [`SDL_AppIterate`] function.
408 /// - `appevent`: the application's [`SDL_AppEvent`] function.
409 /// - `appquit`: the application's [`SDL_AppQuit`] function.
410 ///
411 /// ## Return value
412 /// Returns standard Unix main return value.
413 ///
414 /// ## Thread safety
415 /// It is not safe to call this anywhere except as the only
416 /// function call in [`SDL_main`].
417 ///
418 /// ## Availability
419 /// This function is available since SDL 3.2.0.
420 pub fn SDL_EnterAppMainCallbacks(
421 argc: ::core::ffi::c_int,
422 argv: *mut *mut ::core::ffi::c_char,
423 appinit: SDL_AppInit_func,
424 appiter: SDL_AppIterate_func,
425 appevent: SDL_AppEvent_func,
426 appquit: SDL_AppQuit_func,
427 ) -> ::core::ffi::c_int;
428}
429
430apply_cfg!(#[cfg(any(doc, windows))] => {
431 unsafe extern "C" {
432 /// Register a win32 window class for SDL's use.
433 ///
434 /// This can be called to set the application window class at startup. It is
435 /// safe to call this multiple times, as long as every call is eventually
436 /// paired with a call to [`SDL_UnregisterApp`], but a second registration attempt
437 /// while a previous registration is still active will be ignored, other than
438 /// to increment a counter.
439 ///
440 /// Most applications do not need to, and should not, call this directly; SDL
441 /// will call it when initializing the video subsystem.
442 ///
443 /// If `name` is NULL, SDL currently uses `(CS_BYTEALIGNCLIENT | CS_OWNDC)` for
444 /// the style, regardless of what is specified here.
445 ///
446 /// ## Parameters
447 /// - `name`: the window class name, in UTF-8 encoding. If NULL, SDL
448 /// currently uses "SDL_app" but this isn't guaranteed.
449 /// - `style`: the value to use in WNDCLASSEX::style.
450 /// - `hInst`: the HINSTANCE to use in WNDCLASSEX::hInstance. If zero, SDL
451 /// will use `GetModuleHandle(NULL)` instead.
452 ///
453 /// ## Return value
454 /// Returns true on success or false on failure; call [`SDL_GetError()`] for more
455 /// information.
456 ///
457 /// ## Availability
458 /// This function is available since SDL 3.2.0.
459 pub fn SDL_RegisterApp(name: *const ::core::ffi::c_char, style: Uint32, hInst: *mut ::core::ffi::c_void) -> ::core::primitive::bool;
460 }
461
462 unsafe extern "C" {
463 /// Deregister the win32 window class from an [`SDL_RegisterApp`] call.
464 ///
465 /// This can be called to undo the effects of [`SDL_RegisterApp`].
466 ///
467 /// Most applications do not need to, and should not, call this directly; SDL
468 /// will call it when deinitializing the video subsystem.
469 ///
470 /// It is safe to call this multiple times, as long as every call is eventually
471 /// paired with a prior call to [`SDL_RegisterApp`]. The window class will only be
472 /// deregistered when the registration counter in [`SDL_RegisterApp`] decrements to
473 /// zero through calls to this function.
474 ///
475 /// ## Availability
476 /// This function is available since SDL 3.2.0.
477 pub fn SDL_UnregisterApp();
478 }
479
480});
481
482unsafe extern "C" {
483 /// Callback from the application to let the suspend continue.
484 ///
485 /// This function is only needed for Xbox GDK support; all other platforms will
486 /// do nothing and set an "unsupported" error message.
487 ///
488 /// ## Availability
489 /// This function is available since SDL 3.2.0.
490 pub fn SDL_GDKSuspendComplete();
491}
492
493#[cfg(doc)]
494use crate::everything::*;