Path: blob/master/thirdparty/sdl/include/SDL3/SDL_main.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* # CategoryMain23*24* Redefine main() if necessary so that it is called by SDL.25*26* In order to make this consistent on all platforms, the application's main()27* should look like this:28*29* ```c30* #include <SDL3/SDL.h>31* #include <SDL3/SDL_main.h>32*33* int main(int argc, char *argv[])34* {35* }36* ```37*38* SDL will take care of platform specific details on how it gets called.39*40* This is also where an app can be configured to use the main callbacks, via41* the SDL_MAIN_USE_CALLBACKS macro.42*43* SDL_main.h is a "single-header library," which is to say that including44* this header inserts code into your program, and you should only include it45* once in most cases. SDL.h does not include this header automatically.46*47* For more information, see:48*49* https://wiki.libsdl.org/SDL3/README/main-functions50*/5152#ifndef SDL_main_h_53#define SDL_main_h_5455#include <SDL3/SDL_platform_defines.h>56#include <SDL3/SDL_stdinc.h>57#include <SDL3/SDL_error.h>58#include <SDL3/SDL_events.h>5960#ifdef SDL_WIKI_DOCUMENTATION_SECTION6162/**63* Inform SDL that the app is providing an entry point instead of SDL.64*65* SDL does not define this macro, but will check if it is defined when66* including `SDL_main.h`. If defined, SDL will expect the app to provide the67* proper entry point for the platform, and all the other magic details68* needed, like manually calling SDL_SetMainReady.69*70* Please see [README/main-functions](README/main-functions), (or71* docs/README-main-functions.md in the source tree) for a more detailed72* explanation.73*74* \since This macro is used by the headers since SDL 3.2.0.75*/76#define SDL_MAIN_HANDLED 17778/**79* Inform SDL to use the main callbacks instead of main.80*81* SDL does not define this macro, but will check if it is defined when82* including `SDL_main.h`. If defined, SDL will expect the app to provide83* several functions: SDL_AppInit, SDL_AppEvent, SDL_AppIterate, and84* SDL_AppQuit. The app should not provide a `main` function in this case, and85* doing so will likely cause the build to fail.86*87* Please see [README/main-functions](README/main-functions), (or88* docs/README-main-functions.md in the source tree) for a more detailed89* explanation.90*91* \since This macro is used by the headers since SDL 3.2.0.92*93* \sa SDL_AppInit94* \sa SDL_AppEvent95* \sa SDL_AppIterate96* \sa SDL_AppQuit97*/98#define SDL_MAIN_USE_CALLBACKS 199100/**101* Defined if the target platform offers a special mainline through SDL.102*103* This won't be defined otherwise. If defined, SDL's headers will redefine104* `main` to `SDL_main`.105*106* This macro is defined by `SDL_main.h`, which is not automatically included107* by `SDL.h`.108*109* Even if available, an app can define SDL_MAIN_HANDLED and provide their110* own, if they know what they're doing.111*112* This macro is used internally by SDL, and apps probably shouldn't rely on it.113*114* \since This macro is available since SDL 3.2.0.115*/116#define SDL_MAIN_AVAILABLE117118/**119* Defined if the target platform _requires_ a special mainline through SDL.120*121* This won't be defined otherwise. If defined, SDL's headers will redefine122* `main` to `SDL_main`.123*124* This macro is defined by `SDL_main.h`, which is not automatically included125* by `SDL.h`.126*127* Even if required, an app can define SDL_MAIN_HANDLED and provide their128* own, if they know what they're doing.129*130* This macro is used internally by SDL, and apps probably shouldn't rely on it.131*132* \since This macro is available since SDL 3.2.0.133*/134#define SDL_MAIN_NEEDED135136#endif137138#if defined(__has_include)139#if __has_include("SDL_main_private.h") && __has_include("SDL_main_impl_private.h")140#define SDL_PLATFORM_PRIVATE_MAIN141#endif142#endif143144#ifndef SDL_MAIN_HANDLED145#if defined(SDL_PLATFORM_PRIVATE_MAIN)146/* Private platforms may have their own ideas about entry points. */147#include "SDL_main_private.h"148149#elif defined(SDL_PLATFORM_WIN32)150/* On Windows SDL provides WinMain(), which parses the command line and passes151the arguments to your main function.152153If you provide your own WinMain(), you may define SDL_MAIN_HANDLED154*/155#define SDL_MAIN_AVAILABLE156157#elif defined(SDL_PLATFORM_GDK)158/* On GDK, SDL provides a main function that initializes the game runtime.159160If you prefer to write your own WinMain-function instead of having SDL161provide one that calls your main() function,162#define SDL_MAIN_HANDLED before #include'ing SDL_main.h163and call the SDL_RunApp function from your entry point.164*/165#define SDL_MAIN_NEEDED166167#elif defined(SDL_PLATFORM_IOS)168/* On iOS SDL provides a main function that creates an application delegate169and starts the iOS application run loop.170171To use it, just #include SDL_main.h in the source file that contains your172main() function.173174See src/video/uikit/SDL_uikitappdelegate.m for more details.175*/176#define SDL_MAIN_NEEDED177178#elif defined(SDL_PLATFORM_ANDROID)179/* On Android SDL provides a Java class in SDLActivity.java that is the180main activity entry point.181182See docs/README-android.md for more details on extending that class.183*/184#define SDL_MAIN_NEEDED185186/* As this is launched from Java, the real entry point (main() function)187is outside of the the binary built from this code.188This define makes sure that, unlike on other platforms, SDL_main.h189and SDL_main_impl.h export an `SDL_main()` function (to be called190from Java), but don't implement a native `int main(int argc, char* argv[])`191or similar.192*/193#define SDL_MAIN_EXPORTED194195#elif defined(SDL_PLATFORM_EMSCRIPTEN)196/* On Emscripten, SDL provides a main function that converts URL197parameters that start with "SDL_" to environment variables, so198they can be used as SDL hints, etc.199200This is 100% optional, so if you don't want this to happen, you may201define SDL_MAIN_HANDLED202*/203#define SDL_MAIN_AVAILABLE204205#elif defined(SDL_PLATFORM_PSP)206/* On PSP SDL provides a main function that sets the module info,207activates the GPU and starts the thread required to be able to exit208the software.209210If you provide this yourself, you may define SDL_MAIN_HANDLED211*/212#define SDL_MAIN_AVAILABLE213214#elif defined(SDL_PLATFORM_PS2)215#define SDL_MAIN_AVAILABLE216217#define SDL_PS2_SKIP_IOP_RESET() \218void reset_IOP(); \219void reset_IOP() {}220221#elif defined(SDL_PLATFORM_3DS)222/*223On N3DS, SDL provides a main function that sets up the screens224and storage.225226If you provide this yourself, you may define SDL_MAIN_HANDLED227*/228#define SDL_MAIN_AVAILABLE229230#endif231#endif /* SDL_MAIN_HANDLED */232233234#ifdef SDL_WIKI_DOCUMENTATION_SECTION235236/**237* A macro to tag a main entry point function as exported.238*239* Most platforms don't need this, and the macro will be defined to nothing.240* Some, like Android, keep the entry points in a shared library and need to241* explicitly export the symbols.242*243* External code rarely needs this, and if it needs something, it's almost244* always SDL_DECLSPEC instead.245*246* \since This macro is available since SDL 3.2.0.247*248* \sa SDL_DECLSPEC249*/250#define SDLMAIN_DECLSPEC251252#elif defined(SDL_MAIN_EXPORTED)253/* We need to export SDL_main so it can be launched from external code,254like SDLActivity.java on Android */255#define SDLMAIN_DECLSPEC SDL_DECLSPEC256#else257/* usually this is empty */258#define SDLMAIN_DECLSPEC259#endif /* SDL_MAIN_EXPORTED */260261#if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE) || defined(SDL_MAIN_USE_CALLBACKS)262#define main SDL_main263#endif264265#include <SDL3/SDL_init.h>266#include <SDL3/SDL_begin_code.h>267#ifdef __cplusplus268extern "C" {269#endif270271/*272* You can (optionally!) define SDL_MAIN_USE_CALLBACKS before including273* SDL_main.h, and then your application will _not_ have a standard274* "main" entry point. Instead, it will operate as a collection of275* functions that are called as necessary by the system. On some276* platforms, this is just a layer where SDL drives your program277* instead of your program driving SDL, on other platforms this might278* hook into the OS to manage the lifecycle. Programs on most platforms279* can use whichever approach they prefer, but the decision boils down280* to:281*282* - Using a standard "main" function: this works like it always has for283* the past 50+ years in C programming, and your app is in control.284* - Using the callback functions: this might clean up some code,285* avoid some #ifdef blocks in your program for some platforms, be more286* resource-friendly to the system, and possibly be the primary way to287* access some future platforms (but none require this at the moment).288*289* This is up to the app; both approaches are considered valid and supported290* ways to write SDL apps.291*292* If using the callbacks, don't define a "main" function. Instead, implement293* the functions listed below in your program.294*/295#ifdef SDL_MAIN_USE_CALLBACKS296297/**298* App-implemented initial entry point for SDL_MAIN_USE_CALLBACKS apps.299*300* Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If using a301* standard "main" function, you should not supply this.302*303* This function is called by SDL once, at startup. The function should304* initialize whatever is necessary, possibly create windows and open audio305* devices, etc. The `argc` and `argv` parameters work like they would with a306* standard "main" function.307*308* This function should not go into an infinite mainloop; it should do any309* one-time setup it requires and then return.310*311* The app may optionally assign a pointer to `*appstate`. This pointer will312* be provided on every future call to the other entry points, to allow313* application state to be preserved between functions without the app needing314* to use a global variable. If this isn't set, the pointer will be NULL in315* future entry points.316*317* If this function returns SDL_APP_CONTINUE, the app will proceed to normal318* operation, and will begin receiving repeated calls to SDL_AppIterate and319* SDL_AppEvent for the life of the program. If this function returns320* SDL_APP_FAILURE, SDL will call SDL_AppQuit and terminate the process with321* an exit code that reports an error to the platform. If it returns322* SDL_APP_SUCCESS, SDL calls SDL_AppQuit and terminates with an exit code323* that reports success to the platform.324*325* This function is called by SDL on the main thread.326*327* \param appstate a place where the app can optionally store a pointer for328* future use.329* \param argc the standard ANSI C main's argc; number of elements in `argv`.330* \param argv the standard ANSI C main's argv; array of command line331* arguments.332* \returns SDL_APP_FAILURE to terminate with an error, SDL_APP_SUCCESS to333* terminate with success, SDL_APP_CONTINUE to continue.334*335* \since This function is available since SDL 3.2.0.336*337* \sa SDL_AppIterate338* \sa SDL_AppEvent339* \sa SDL_AppQuit340*/341extern SDLMAIN_DECLSPEC SDL_AppResult SDLCALL SDL_AppInit(void **appstate, int argc, char *argv[]);342343/**344* App-implemented iteration entry point for SDL_MAIN_USE_CALLBACKS apps.345*346* Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If using a347* standard "main" function, you should not supply this.348*349* This function is called repeatedly by SDL after SDL_AppInit returns 0. The350* function should operate as a single iteration the program's primary loop;351* it should update whatever state it needs and draw a new frame of video,352* usually.353*354* On some platforms, this function will be called at the refresh rate of the355* display (which might change during the life of your app!). There are no356* promises made about what frequency this function might run at. You should357* use SDL's timer functions if you need to see how much time has passed since358* the last iteration.359*360* There is no need to process the SDL event queue during this function; SDL361* will send events as they arrive in SDL_AppEvent, and in most cases the362* event queue will be empty when this function runs anyhow.363*364* This function should not go into an infinite mainloop; it should do one365* iteration of whatever the program does and return.366*367* The `appstate` parameter is an optional pointer provided by the app during368* SDL_AppInit(). If the app never provided a pointer, this will be NULL.369*370* If this function returns SDL_APP_CONTINUE, the app will continue normal371* operation, receiving repeated calls to SDL_AppIterate and SDL_AppEvent for372* the life of the program. If this function returns SDL_APP_FAILURE, SDL will373* call SDL_AppQuit and terminate the process with an exit code that reports374* an error to the platform. If it returns SDL_APP_SUCCESS, SDL calls375* SDL_AppQuit and terminates with an exit code that reports success to the376* platform.377*378* This function is called by SDL on the main thread.379*380* \param appstate an optional pointer, provided by the app in SDL_AppInit.381* \returns SDL_APP_FAILURE to terminate with an error, SDL_APP_SUCCESS to382* terminate with success, SDL_APP_CONTINUE to continue.383*384* \threadsafety This function may get called concurrently with SDL_AppEvent()385* for events not pushed on the main thread.386*387* \since This function is available since SDL 3.2.0.388*389* \sa SDL_AppInit390* \sa SDL_AppEvent391*/392extern SDLMAIN_DECLSPEC SDL_AppResult SDLCALL SDL_AppIterate(void *appstate);393394/**395* App-implemented event entry point for SDL_MAIN_USE_CALLBACKS apps.396*397* Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If using a398* standard "main" function, you should not supply this.399*400* This function is called as needed by SDL after SDL_AppInit returns401* SDL_APP_CONTINUE. It is called once for each new event.402*403* There is (currently) no guarantee about what thread this will be called404* from; whatever thread pushes an event onto SDL's queue will trigger this405* function. SDL is responsible for pumping the event queue between each call406* to SDL_AppIterate, so in normal operation one should only get events in a407* serial fashion, but be careful if you have a thread that explicitly calls408* SDL_PushEvent. SDL itself will push events to the queue on the main thread.409*410* Events sent to this function are not owned by the app; if you need to save411* the data, you should copy it.412*413* This function should not go into an infinite mainloop; it should handle the414* provided event appropriately and return.415*416* The `appstate` parameter is an optional pointer provided by the app during417* SDL_AppInit(). If the app never provided a pointer, this will be NULL.418*419* If this function returns SDL_APP_CONTINUE, the app will continue normal420* operation, receiving repeated calls to SDL_AppIterate and SDL_AppEvent for421* the life of the program. If this function returns SDL_APP_FAILURE, SDL will422* call SDL_AppQuit and terminate the process with an exit code that reports423* an error to the platform. If it returns SDL_APP_SUCCESS, SDL calls424* SDL_AppQuit and terminates with an exit code that reports success to the425* platform.426*427* \param appstate an optional pointer, provided by the app in SDL_AppInit.428* \param event the new event for the app to examine.429* \returns SDL_APP_FAILURE to terminate with an error, SDL_APP_SUCCESS to430* terminate with success, SDL_APP_CONTINUE to continue.431*432* \threadsafety This function may get called concurrently with433* SDL_AppIterate() or SDL_AppQuit() for events not pushed from434* the main thread.435*436* \since This function is available since SDL 3.2.0.437*438* \sa SDL_AppInit439* \sa SDL_AppIterate440*/441extern SDLMAIN_DECLSPEC SDL_AppResult SDLCALL SDL_AppEvent(void *appstate, SDL_Event *event);442443/**444* App-implemented deinit entry point for SDL_MAIN_USE_CALLBACKS apps.445*446* Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If using a447* standard "main" function, you should not supply this.448*449* This function is called once by SDL before terminating the program.450*451* This function will be called no matter what, even if SDL_AppInit requests452* termination.453*454* This function should not go into an infinite mainloop; it should455* deinitialize any resources necessary, perform whatever shutdown activities,456* and return.457*458* You do not need to call SDL_Quit() in this function, as SDL will call it459* after this function returns and before the process terminates, but it is460* safe to do so.461*462* The `appstate` parameter is an optional pointer provided by the app during463* SDL_AppInit(). If the app never provided a pointer, this will be NULL. This464* function call is the last time this pointer will be provided, so any465* resources to it should be cleaned up here.466*467* This function is called by SDL on the main thread.468*469* \param appstate an optional pointer, provided by the app in SDL_AppInit.470* \param result the result code that terminated the app (success or failure).471*472* \threadsafety SDL_AppEvent() may get called concurrently with this function473* if other threads that push events are still active.474*475* \since This function is available since SDL 3.2.0.476*477* \sa SDL_AppInit478*/479extern SDLMAIN_DECLSPEC void SDLCALL SDL_AppQuit(void *appstate, SDL_AppResult result);480481#endif /* SDL_MAIN_USE_CALLBACKS */482483484/**485* The prototype for the application's main() function486*487* \param argc an ANSI-C style main function's argc.488* \param argv an ANSI-C style main function's argv.489* \returns an ANSI-C main return code; generally 0 is considered successful490* program completion, and small non-zero values are considered491* errors.492*493* \since This datatype is available since SDL 3.2.0.494*/495typedef int (SDLCALL *SDL_main_func)(int argc, char *argv[]);496497/**498* An app-supplied function for program entry.499*500* Apps do not directly create this function; they should create a standard501* ANSI-C `main` function instead. If SDL needs to insert some startup code502* before `main` runs, or the platform doesn't actually _use_ a function503* called "main", SDL will do some macro magic to redefine `main` to504* `SDL_main` and provide its own `main`.505*506* Apps should include `SDL_main.h` in the same file as their `main` function,507* and they should not use that symbol for anything else in that file, as it508* might get redefined.509*510* This function is only provided by the app if it isn't using511* SDL_MAIN_USE_CALLBACKS.512*513* Program startup is a surprisingly complex topic. Please see514* [README/main-functions](README/main-functions), (or515* docs/README-main-functions.md in the source tree) for a more detailed516* explanation.517*518* \param argc an ANSI-C style main function's argc.519* \param argv an ANSI-C style main function's argv.520* \returns an ANSI-C main return code; generally 0 is considered successful521* program completion, and small non-zero values are considered522* errors.523*524* \threadsafety This is the program entry point.525*526* \since This function is available since SDL 3.2.0.527*/528extern SDLMAIN_DECLSPEC int SDLCALL SDL_main(int argc, char *argv[]);529530/**531* Circumvent failure of SDL_Init() when not using SDL_main() as an entry532* point.533*534* This function is defined in SDL_main.h, along with the preprocessor rule to535* redefine main() as SDL_main(). Thus to ensure that your main() function536* will not be changed it is necessary to define SDL_MAIN_HANDLED before537* including SDL.h.538*539* \since This function is available since SDL 3.2.0.540*541* \sa SDL_Init542*/543extern SDL_DECLSPEC void SDLCALL SDL_SetMainReady(void);544545/**546* Initializes and launches an SDL application, by doing platform-specific547* initialization before calling your mainFunction and cleanups after it548* returns, if that is needed for a specific platform, otherwise it just calls549* mainFunction.550*551* You can use this if you want to use your own main() implementation without552* using SDL_main (like when using SDL_MAIN_HANDLED). When using this, you do553* *not* need SDL_SetMainReady().554*555* \param argc the argc parameter from the application's main() function, or 0556* if the platform's main-equivalent has no argc.557* \param argv the argv parameter from the application's main() function, or558* NULL if the platform's main-equivalent has no argv.559* \param mainFunction your SDL app's C-style main(). NOT the function you're560* calling this from! Its name doesn't matter; it doesn't561* literally have to be `main`.562* \param reserved should be NULL (reserved for future use, will probably be563* platform-specific then).564* \returns the return value from mainFunction: 0 on success, otherwise565* failure; SDL_GetError() might have more information on the566* failure.567*568* \threadsafety Generally this is called once, near startup, from the569* process's initial thread.570*571* \since This function is available since SDL 3.2.0.572*/573extern SDL_DECLSPEC int SDLCALL SDL_RunApp(int argc, char *argv[], SDL_main_func mainFunction, void *reserved);574575/**576* An entry point for SDL's use in SDL_MAIN_USE_CALLBACKS.577*578* Generally, you should not call this function directly. This only exists to579* hand off work into SDL as soon as possible, where it has a lot more control580* and functionality available, and make the inline code in SDL_main.h as581* small as possible.582*583* Not all platforms use this, it's actual use is hidden in a magic584* header-only library, and you should not call this directly unless you585* _really_ know what you're doing.586*587* \param argc standard Unix main argc.588* \param argv standard Unix main argv.589* \param appinit the application's SDL_AppInit function.590* \param appiter the application's SDL_AppIterate function.591* \param appevent the application's SDL_AppEvent function.592* \param appquit the application's SDL_AppQuit function.593* \returns standard Unix main return value.594*595* \threadsafety It is not safe to call this anywhere except as the only596* function call in SDL_main.597*598* \since This function is available since SDL 3.2.0.599*/600extern SDL_DECLSPEC int SDLCALL SDL_EnterAppMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit);601602603#if defined(SDL_PLATFORM_WINDOWS)604605/**606* Register a win32 window class for SDL's use.607*608* This can be called to set the application window class at startup. It is609* safe to call this multiple times, as long as every call is eventually610* paired with a call to SDL_UnregisterApp, but a second registration attempt611* while a previous registration is still active will be ignored, other than612* to increment a counter.613*614* Most applications do not need to, and should not, call this directly; SDL615* will call it when initializing the video subsystem.616*617* \param name the window class name, in UTF-8 encoding. If NULL, SDL618* currently uses "SDL_app" but this isn't guaranteed.619* \param style the value to use in WNDCLASSEX::style. If `name` is NULL, SDL620* currently uses `(CS_BYTEALIGNCLIENT | CS_OWNDC)` regardless of621* what is specified here.622* \param hInst the HINSTANCE to use in WNDCLASSEX::hInstance. If zero, SDL623* will use `GetModuleHandle(NULL)` instead.624* \returns true on success or false on failure; call SDL_GetError() for more625* information.626*627* \since This function is available since SDL 3.2.0.628*/629extern SDL_DECLSPEC bool SDLCALL SDL_RegisterApp(const char *name, Uint32 style, void *hInst);630631/**632* Deregister the win32 window class from an SDL_RegisterApp call.633*634* This can be called to undo the effects of SDL_RegisterApp.635*636* Most applications do not need to, and should not, call this directly; SDL637* will call it when deinitializing the video subsystem.638*639* It is safe to call this multiple times, as long as every call is eventually640* paired with a prior call to SDL_RegisterApp. The window class will only be641* deregistered when the registration counter in SDL_RegisterApp decrements to642* zero through calls to this function.643*644* \since This function is available since SDL 3.2.0.645*/646extern SDL_DECLSPEC void SDLCALL SDL_UnregisterApp(void);647648#endif /* defined(SDL_PLATFORM_WINDOWS) */649650/**651* Callback from the application to let the suspend continue.652*653* This function is only needed for Xbox GDK support; all other platforms will654* do nothing and set an "unsupported" error message.655*656* \since This function is available since SDL 3.2.0.657*/658extern SDL_DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);659660#ifdef __cplusplus661}662#endif663664#include <SDL3/SDL_close_code.h>665666#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)667/* include header-only SDL_main implementations */668#if defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)669/* platforms which main (-equivalent) can be implemented in plain C */670#include <SDL3/SDL_main_impl.h>671#endif672#endif673674#endif /* SDL_main_h_ */675676677