Programmatically exit an Application with ExitEvent triggered
mosra opened this issue · 0 comments
Simple request with a nontrivial solution, as usual. Right now, calling exit()
on GLFW/SDL2 application just stops executing the main loop, which unconditionally leads to resource cleanup and exiting the program. In particular, the ExitEvent
, allowing the exit to be aborted, is not fired here. That means there's no builtin way to request application exit via, say, a button in the application UI and have it behave the same way as if the window close button was clicked.
In case of SDL2, this behavior can be implemented by manually pushing an exit event instead of calling exit()
, however that doesn't allow the user to specify the exit code:
SDL_Event quit{};
quit.type = SDL_QUIT;
SDL_PushEvent(&quit);
In case of GLFW, there unfortunately doesn't seem to be a builtin way, glfwSetWindowShouldClose(window(), true)
leads to an unconditional stop of the event loop, with the callback from glfwSetWindowCloseCallback()
never entered. Possibly related issue that could allow posting "fake" events to implement this: glfw/glfw#688
Alternatively, this could be implemented outside of SDL/GLFW, simply by firing the ExitEvent
if an exit is requested, and then possibly cancelling the exit.
Things to do:
- Document the current behavior, so it's clear
exit()
sidesteps theExitEvent
- Implement this for SDL and GLFW, outside of their event loop to have common handling for both
- Having
ExitEvent
fired fromexit()
might not make sense in all cases, especially when the app wants to unconditionally exit due to some unrecovarable failure, or from a constructor (or maybe from the constructor it wouldn't trigger the event?), so maybe a newrequestExit()
API with this behavior might make sense instead?
- Having
- Possibly related: on iOS OTOH, the
exit()
doesn't seem to do an actual quit, just stops event processing: https://gitter.im/mosra/magnum/archives/2022/01/11?at=61dcc94b9b470f38975db494