Hey,
Event Watch handler is called in SDL_PushEvent() (see SDL_events.c code )
{will, Did}_Enter_{Back, Fore}ground_event are not pushed anymore as event, and expect a Watch Event (see SDL_SendAppEvent in SDL_events.c)
Some internal:
On Android, SDL works with 2 threads (and also others, but we don’t care here):
- android SDLActivity thread: the java side, that gets all system and environment information.
- main C thread: you app and 99% of SDL library code.
(others thread are SDL audio, video capture, and all android system drivers thread).
So SDLActivity is a listener of all system events and has to send them to the main C thread and SDL lib code. SDLActivity has some SDL code to that: for instance it can use SDL_PushEvent() to send an event (like FINGER_MOTION or SDL_APP_WILLENTERBACKGROUND) that you will get with SDL_PollEvent().
that’s fine because SDL_PushEvent() is thread safe with SDL_PollEvent();
but now we add Event Watch:
so when a SDL_PushEvent() is called, an event watch callback might be triggered.
there are not so many EventWatch in SDL internal. (max 5 in SDL3, depends on configuration).
SDL_RendererEventWatch is maybe the oldest one, in SDL3 it depends on:
922 if (event->type == SDL_EVENT_WINDOW_RESIZED ||
923 event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED ||
924 …
929 } else if (event->type == SDL_EVENT_WINDOW_HIDDEN) {
…
931 } else if (event->type == SDL_EVENT_WINDOW_SHOWN) {
932 …
935 } else if (event->type == SDL_EVENT_WINDOW_MINIMIZED) {
…
937 } else if (event->type == SDL_EVENT_WINDOW_RESTORED ||
938 event->type == SDL_EVENT_WINDOW_MAXIMIZED) {
939 …
942 } else if (event->type == SDL_EVENT_WINDOW_DISPLAY_CHANGED ||
943 event->type == SDL_EVENT_WINDOW_HDR_STATE_CHANGED) {
…
}
that’s not so much ok, because it’s not clear if everything there (the … ) is thread-safe or not. but probably it is !
I you have your own event watch for RESIZED, you have to make sure it is thread safe with your code.
For instance: on Android the RESIZED event can be sent from SDLActivity thread, in onNativeResize() method.
so in that case, the EventWatch is called within SDLActivity thread. and it’s critical that you make it thread safe.
For SDL_APP_WILLENTERBACKGROUND, I think the thread safety will be always ok, because there are some other new internal “lifecycle” event that are sent from SDLActivity to C thread. so SDL push events (and so the EventWatch handler) are done in the main C thread.
(and in fact, you have to use EventWatch event, because I think the BG Event are not pushed anymore).
( but, maybe, here it misses some handshaking / acknowledge in the distribution if those likecylce events
. the activity may continue whereas the event are not processed yet. )
hope it helps,