emscripten: Send SDL_APP_TERMINATING before unload (#88)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
diff --git a/src/video/emscripten/SDL_emscriptenevents.c b/src/video/emscripten/SDL_emscriptenevents.c
index ac17fb1..6376580 100644
--- a/src/video/emscripten/SDL_emscriptenevents.c
+++ b/src/video/emscripten/SDL_emscriptenevents.c
@@ -645,6 +645,16 @@ Emscripten_HandleVisibilityChange(int eventType, const EmscriptenVisibilityChang
return 0;
}
+static const char*
+Emscripten_HandleBeforeUnload(int eventType, const void *reserved, void *userData)
+{
+ /* This event will need to be handled synchronously, e.g. using
+ SDL_AddEventWatch, as the page is being closed *now*. */
+ /* No need to send a SDL_QUIT, the app won't get control again. */
+ SDL_SendAppEvent(SDL_APP_TERMINATING);
+ return ""; /* don't trigger confirmation dialog */
+}
+
void
Emscripten_RegisterEventHandlers(SDL_WindowData *data)
{
@@ -684,6 +694,8 @@ Emscripten_RegisterEventHandlers(SDL_WindowData *data)
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, data, 0, Emscripten_HandleResize);
emscripten_set_visibilitychange_callback(data, 0, Emscripten_HandleVisibilityChange);
+
+ emscripten_set_beforeunload_callback(data, Emscripten_HandleBeforeUnload);
}
void
@@ -726,6 +738,8 @@ Emscripten_UnregisterEventHandlers(SDL_WindowData *data)
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 0, NULL);
emscripten_set_visibilitychange_callback(NULL, 0, NULL);
+
+ emscripten_set_beforeunload_callback(NULL, NULL);
}
#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */