Skip to content

Commit

Permalink
emscripten: Send SDL_APP_TERMINATING before unload (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beuc committed Apr 9, 2020
1 parent 0380cbd commit 44716b0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/video/emscripten/SDL_emscriptenevents.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 44716b0

Please sign in to comment.