Commit bdca510fd69908487365ab6f65a3b45cc56e11fb

Sam Lantinga 2016-09-13T00:03:28

simplify fullscreen handling using new fullscreen_strategy api, patch contributed by Charlie Birks

diff --git a/src/video/emscripten/SDL_emscriptenevents.c b/src/video/emscripten/SDL_emscriptenevents.c
index 74a4393..02a5478 100644
--- a/src/video/emscripten/SDL_emscriptenevents.c
+++ b/src/video/emscripten/SDL_emscriptenevents.c
@@ -472,59 +472,19 @@ Emscripten_HandleKeyPress(int eventType, const EmscriptenKeyboardEvent *keyEvent
 EM_BOOL
 Emscripten_HandleFullscreenChange(int eventType, const EmscriptenFullscreenChangeEvent *fullscreenChangeEvent, void *userData)
 {
-    /*make sure this is actually our element going fullscreen*/
-    if(SDL_strcmp(fullscreenChangeEvent->id, "SDLFullscreenElement") != 0)
-        return 0;
-
     SDL_WindowData *window_data = userData;
     if(fullscreenChangeEvent->isFullscreen)
     {
         SDL_bool is_desktop_fullscreen;
         window_data->window->flags |= window_data->requested_fullscreen_mode;
 
-        if(!window_data->requested_fullscreen_mode)
-            window_data->window->flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; /*we didn't reqest fullscreen*/
-
         window_data->requested_fullscreen_mode = 0;
 
-        is_desktop_fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
-
-        /*update size*/
-        if(window_data->window->flags & SDL_WINDOW_RESIZABLE || is_desktop_fullscreen)
-        {
-            emscripten_set_canvas_size(fullscreenChangeEvent->screenWidth, fullscreenChangeEvent->screenHeight);
-            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, fullscreenChangeEvent->screenWidth, fullscreenChangeEvent->screenHeight);
-        }
-        else
-        {
-            /*preserve ratio*/
-            double w = window_data->window->w;
-            double h = window_data->window->h;
-            double factor = SDL_min(fullscreenChangeEvent->screenWidth / w, fullscreenChangeEvent->screenHeight / h);
-            emscripten_set_element_css_size(NULL, w * factor, h * factor);
-        }
+        if(!window_data->requested_fullscreen_mode)
+            window_data->window->flags |= SDL_WINDOW_FULLSCREEN; /*we didn't reqest fullscreen*/
     }
     else
     {
-        EM_ASM({
-            //un-reparent canvas (similar to Module.requestFullscreen)
-            var canvas = Module['canvas'];
-            if(canvas.parentNode.id == "SDLFullscreenElement") {
-                var canvasContainer = canvas.parentNode;
-                canvasContainer.parentNode.insertBefore(canvas, canvasContainer);
-                canvasContainer.parentNode.removeChild(canvasContainer);
-            }
-        });
-        double unscaled_w = window_data->windowed_width / window_data->pixel_ratio;
-        double unscaled_h = window_data->windowed_height / window_data->pixel_ratio;
-        emscripten_set_canvas_size(window_data->windowed_width, window_data->windowed_height);
-
-        if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
-            emscripten_set_element_css_size(NULL, unscaled_w, unscaled_h);
-        }
-
-        SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, unscaled_w, unscaled_h);
-
         window_data->window->flags &= ~FULLSCREEN_MASK;
     }
 
@@ -535,17 +495,7 @@ EM_BOOL
 Emscripten_HandleResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
 {
     SDL_WindowData *window_data = userData;
-    if(window_data->window->flags & FULLSCREEN_MASK)
-    {
-        SDL_bool is_desktop_fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
-
-        if(window_data->window->flags & SDL_WINDOW_RESIZABLE || is_desktop_fullscreen)
-        {
-            emscripten_set_canvas_size(uiEvent->windowInnerWidth * window_data->pixel_ratio, uiEvent->windowInnerHeight * window_data->pixel_ratio);
-            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, uiEvent->windowInnerWidth, uiEvent->windowInnerHeight);
-        }
-    }
-    else
+    if(!(window_data->window->flags & FULLSCREEN_MASK))
     {
         /* this will only work if the canvas size is set through css */
         if(window_data->window->flags & SDL_WINDOW_RESIZABLE)
@@ -572,6 +522,22 @@ Emscripten_HandleResize(int eventType, const EmscriptenUiEvent *uiEvent, void *u
 }
 
 EM_BOOL
+Emscripten_HandleCanvasResize(int eventType, const void *reserved, void *userData)
+{
+    /*this is used during fullscreen changes*/
+    SDL_WindowData *window_data = userData;
+
+    if(window_data->fullscreen_resize)
+    {
+        double css_w, css_h;
+        emscripten_get_element_css_size(NULL, &css_w, &css_h);
+        SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
+    }
+
+    return 0;
+}
+
+EM_BOOL
 Emscripten_HandleVisibilityChange(int eventType, const EmscriptenVisibilityChangeEvent *visEvent, void *userData)
 {
     SDL_WindowData *window_data = userData;
diff --git a/src/video/emscripten/SDL_emscriptenevents.h b/src/video/emscripten/SDL_emscriptenevents.h
index d5b65f8..089ff60 100644
--- a/src/video/emscripten/SDL_emscriptenevents.h
+++ b/src/video/emscripten/SDL_emscriptenevents.h
@@ -30,6 +30,9 @@ Emscripten_RegisterEventHandlers(SDL_WindowData *data);
 
 extern void
 Emscripten_UnregisterEventHandlers(SDL_WindowData *data);
+
+extern int
+Emscripten_HandleCanvasResize(int eventType, const void *reserved, void *userData);
 #endif /* _SDL_emscriptenevents_h */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/emscripten/SDL_emscriptenvideo.c b/src/video/emscripten/SDL_emscriptenvideo.c
index 302ca87..ef418cf 100644
--- a/src/video/emscripten/SDL_emscriptenvideo.c
+++ b/src/video/emscripten/SDL_emscriptenvideo.c
@@ -218,9 +218,6 @@ Emscripten_CreateWindow(_THIS, SDL_Window * window)
         }
     }
 
-    wdata->windowed_width = scaled_w;
-    wdata->windowed_height = scaled_h;
-
     if (window->flags & SDL_WINDOW_OPENGL) {
         if (!_this->egl_data) {
             if (SDL_GL_LoadLibrary(NULL) < 0) {
@@ -290,24 +287,30 @@ Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * di
         data = (SDL_WindowData *) window->driverdata;
 
         if(fullscreen) {
+            EmscriptenFullscreenStrategy strategy;
+            SDL_bool is_desktop_fullscreen = (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
+
+            strategy.scaleMode = is_desktop_fullscreen ? EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH : EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT;
+
+            if(!is_desktop_fullscreen) {
+                strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE;
+            } else if(window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
+                strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_HIDEF;
+            } else {
+                strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
+            }
+
+            strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
+
+            strategy.canvasResizedCallback = Emscripten_HandleCanvasResize;
+            strategy.canvasResizedCallbackUserData = data;
+
             data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
+            data->fullscreen_resize = is_desktop_fullscreen;
             /*unset the fullscreen flags as we're not actually fullscreen yet*/
             window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
 
-            EM_ASM({
-                //reparent canvas (similar to Module.requestFullscreen)
-                var canvas = Module['canvas'];
-                if(canvas.parentNode.id != "SDLFullscreenElement") {
-                    var canvasContainer = document.createElement("div");
-                    canvasContainer.id = "SDLFullscreenElement";
-                    canvas.parentNode.insertBefore(canvasContainer, canvas);
-                    canvasContainer.appendChild(canvas);
-                }
-            });
-
-            int is_fullscreen;
-            emscripten_get_canvas_size(&data->windowed_width, &data->windowed_height, &is_fullscreen);
-            emscripten_request_fullscreen("SDLFullscreenElement", 1);
+            emscripten_request_fullscreen_strategy(NULL, 1, &strategy);
         }
         else
             emscripten_exit_fullscreen();
diff --git a/src/video/emscripten/SDL_emscriptenvideo.h b/src/video/emscripten/SDL_emscriptenvideo.h
index 49b0b35..7618ab6 100644
--- a/src/video/emscripten/SDL_emscriptenvideo.h
+++ b/src/video/emscripten/SDL_emscriptenvideo.h
@@ -38,14 +38,12 @@ typedef struct SDL_WindowData
     SDL_Window *window;
     SDL_Surface *surface;
 
-    int windowed_width;
-    int windowed_height;
-
     float pixel_ratio;
 
     SDL_bool external_size;
 
     int requested_fullscreen_mode;
+    SDL_bool fullscreen_resize;
 
     SDL_bool finger_touching;  /* for mapping touch events to mice */
     SDL_FingerID first_finger;