simplify fullscreen handling using new fullscreen_strategy api, patch contributed by Charlie Birks
authorSam Lantinga <slouken@libsdl.org>
Tue, 13 Sep 2016 00:03:28 -0700
changeset 103174a45690d5d82
parent 10316 76bcc22dc5e4
child 10318 7b9ae714c507
simplify fullscreen handling using new fullscreen_strategy api, patch contributed by Charlie Birks
src/video/emscripten/SDL_emscriptenevents.c
src/video/emscripten/SDL_emscriptenevents.h
src/video/emscripten/SDL_emscriptenvideo.c
src/video/emscripten/SDL_emscriptenvideo.h
     1.1 --- a/src/video/emscripten/SDL_emscriptenevents.c	Mon Sep 12 23:58:08 2016 -0700
     1.2 +++ b/src/video/emscripten/SDL_emscriptenevents.c	Tue Sep 13 00:03:28 2016 -0700
     1.3 @@ -472,59 +472,19 @@
     1.4  EM_BOOL
     1.5  Emscripten_HandleFullscreenChange(int eventType, const EmscriptenFullscreenChangeEvent *fullscreenChangeEvent, void *userData)
     1.6  {
     1.7 -    /*make sure this is actually our element going fullscreen*/
     1.8 -    if(SDL_strcmp(fullscreenChangeEvent->id, "SDLFullscreenElement") != 0)
     1.9 -        return 0;
    1.10 -
    1.11      SDL_WindowData *window_data = userData;
    1.12      if(fullscreenChangeEvent->isFullscreen)
    1.13      {
    1.14          SDL_bool is_desktop_fullscreen;
    1.15          window_data->window->flags |= window_data->requested_fullscreen_mode;
    1.16  
    1.17 -        if(!window_data->requested_fullscreen_mode)
    1.18 -            window_data->window->flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; /*we didn't reqest fullscreen*/
    1.19 -
    1.20          window_data->requested_fullscreen_mode = 0;
    1.21  
    1.22 -        is_desktop_fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
    1.23 -
    1.24 -        /*update size*/
    1.25 -        if(window_data->window->flags & SDL_WINDOW_RESIZABLE || is_desktop_fullscreen)
    1.26 -        {
    1.27 -            emscripten_set_canvas_size(fullscreenChangeEvent->screenWidth, fullscreenChangeEvent->screenHeight);
    1.28 -            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, fullscreenChangeEvent->screenWidth, fullscreenChangeEvent->screenHeight);
    1.29 -        }
    1.30 -        else
    1.31 -        {
    1.32 -            /*preserve ratio*/
    1.33 -            double w = window_data->window->w;
    1.34 -            double h = window_data->window->h;
    1.35 -            double factor = SDL_min(fullscreenChangeEvent->screenWidth / w, fullscreenChangeEvent->screenHeight / h);
    1.36 -            emscripten_set_element_css_size(NULL, w * factor, h * factor);
    1.37 -        }
    1.38 +        if(!window_data->requested_fullscreen_mode)
    1.39 +            window_data->window->flags |= SDL_WINDOW_FULLSCREEN; /*we didn't reqest fullscreen*/
    1.40      }
    1.41      else
    1.42      {
    1.43 -        EM_ASM({
    1.44 -            //un-reparent canvas (similar to Module.requestFullscreen)
    1.45 -            var canvas = Module['canvas'];
    1.46 -            if(canvas.parentNode.id == "SDLFullscreenElement") {
    1.47 -                var canvasContainer = canvas.parentNode;
    1.48 -                canvasContainer.parentNode.insertBefore(canvas, canvasContainer);
    1.49 -                canvasContainer.parentNode.removeChild(canvasContainer);
    1.50 -            }
    1.51 -        });
    1.52 -        double unscaled_w = window_data->windowed_width / window_data->pixel_ratio;
    1.53 -        double unscaled_h = window_data->windowed_height / window_data->pixel_ratio;
    1.54 -        emscripten_set_canvas_size(window_data->windowed_width, window_data->windowed_height);
    1.55 -
    1.56 -        if (!window_data->external_size && window_data->pixel_ratio != 1.0f) {
    1.57 -            emscripten_set_element_css_size(NULL, unscaled_w, unscaled_h);
    1.58 -        }
    1.59 -
    1.60 -        SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, unscaled_w, unscaled_h);
    1.61 -
    1.62          window_data->window->flags &= ~FULLSCREEN_MASK;
    1.63      }
    1.64  
    1.65 @@ -535,17 +495,7 @@
    1.66  Emscripten_HandleResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
    1.67  {
    1.68      SDL_WindowData *window_data = userData;
    1.69 -    if(window_data->window->flags & FULLSCREEN_MASK)
    1.70 -    {
    1.71 -        SDL_bool is_desktop_fullscreen = (window_data->window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
    1.72 -
    1.73 -        if(window_data->window->flags & SDL_WINDOW_RESIZABLE || is_desktop_fullscreen)
    1.74 -        {
    1.75 -            emscripten_set_canvas_size(uiEvent->windowInnerWidth * window_data->pixel_ratio, uiEvent->windowInnerHeight * window_data->pixel_ratio);
    1.76 -            SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, uiEvent->windowInnerWidth, uiEvent->windowInnerHeight);
    1.77 -        }
    1.78 -    }
    1.79 -    else
    1.80 +    if(!(window_data->window->flags & FULLSCREEN_MASK))
    1.81      {
    1.82          /* this will only work if the canvas size is set through css */
    1.83          if(window_data->window->flags & SDL_WINDOW_RESIZABLE)
    1.84 @@ -572,6 +522,22 @@
    1.85  }
    1.86  
    1.87  EM_BOOL
    1.88 +Emscripten_HandleCanvasResize(int eventType, const void *reserved, void *userData)
    1.89 +{
    1.90 +    /*this is used during fullscreen changes*/
    1.91 +    SDL_WindowData *window_data = userData;
    1.92 +
    1.93 +    if(window_data->fullscreen_resize)
    1.94 +    {
    1.95 +        double css_w, css_h;
    1.96 +        emscripten_get_element_css_size(NULL, &css_w, &css_h);
    1.97 +        SDL_SendWindowEvent(window_data->window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
    1.98 +    }
    1.99 +
   1.100 +    return 0;
   1.101 +}
   1.102 +
   1.103 +EM_BOOL
   1.104  Emscripten_HandleVisibilityChange(int eventType, const EmscriptenVisibilityChangeEvent *visEvent, void *userData)
   1.105  {
   1.106      SDL_WindowData *window_data = userData;
     2.1 --- a/src/video/emscripten/SDL_emscriptenevents.h	Mon Sep 12 23:58:08 2016 -0700
     2.2 +++ b/src/video/emscripten/SDL_emscriptenevents.h	Tue Sep 13 00:03:28 2016 -0700
     2.3 @@ -30,6 +30,9 @@
     2.4  
     2.5  extern void
     2.6  Emscripten_UnregisterEventHandlers(SDL_WindowData *data);
     2.7 +
     2.8 +extern int
     2.9 +Emscripten_HandleCanvasResize(int eventType, const void *reserved, void *userData);
    2.10  #endif /* _SDL_emscriptenevents_h */
    2.11  
    2.12  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/emscripten/SDL_emscriptenvideo.c	Mon Sep 12 23:58:08 2016 -0700
     3.2 +++ b/src/video/emscripten/SDL_emscriptenvideo.c	Tue Sep 13 00:03:28 2016 -0700
     3.3 @@ -218,9 +218,6 @@
     3.4          }
     3.5      }
     3.6  
     3.7 -    wdata->windowed_width = scaled_w;
     3.8 -    wdata->windowed_height = scaled_h;
     3.9 -
    3.10      if (window->flags & SDL_WINDOW_OPENGL) {
    3.11          if (!_this->egl_data) {
    3.12              if (SDL_GL_LoadLibrary(NULL) < 0) {
    3.13 @@ -290,24 +287,30 @@
    3.14          data = (SDL_WindowData *) window->driverdata;
    3.15  
    3.16          if(fullscreen) {
    3.17 +            EmscriptenFullscreenStrategy strategy;
    3.18 +            SDL_bool is_desktop_fullscreen = (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
    3.19 +
    3.20 +            strategy.scaleMode = is_desktop_fullscreen ? EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH : EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT;
    3.21 +
    3.22 +            if(!is_desktop_fullscreen) {
    3.23 +                strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE;
    3.24 +            } else if(window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
    3.25 +                strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_HIDEF;
    3.26 +            } else {
    3.27 +                strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
    3.28 +            }
    3.29 +
    3.30 +            strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
    3.31 +
    3.32 +            strategy.canvasResizedCallback = Emscripten_HandleCanvasResize;
    3.33 +            strategy.canvasResizedCallbackUserData = data;
    3.34 +
    3.35              data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
    3.36 +            data->fullscreen_resize = is_desktop_fullscreen;
    3.37              /*unset the fullscreen flags as we're not actually fullscreen yet*/
    3.38              window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
    3.39  
    3.40 -            EM_ASM({
    3.41 -                //reparent canvas (similar to Module.requestFullscreen)
    3.42 -                var canvas = Module['canvas'];
    3.43 -                if(canvas.parentNode.id != "SDLFullscreenElement") {
    3.44 -                    var canvasContainer = document.createElement("div");
    3.45 -                    canvasContainer.id = "SDLFullscreenElement";
    3.46 -                    canvas.parentNode.insertBefore(canvasContainer, canvas);
    3.47 -                    canvasContainer.appendChild(canvas);
    3.48 -                }
    3.49 -            });
    3.50 -
    3.51 -            int is_fullscreen;
    3.52 -            emscripten_get_canvas_size(&data->windowed_width, &data->windowed_height, &is_fullscreen);
    3.53 -            emscripten_request_fullscreen("SDLFullscreenElement", 1);
    3.54 +            emscripten_request_fullscreen_strategy(NULL, 1, &strategy);
    3.55          }
    3.56          else
    3.57              emscripten_exit_fullscreen();
     4.1 --- a/src/video/emscripten/SDL_emscriptenvideo.h	Mon Sep 12 23:58:08 2016 -0700
     4.2 +++ b/src/video/emscripten/SDL_emscriptenvideo.h	Tue Sep 13 00:03:28 2016 -0700
     4.3 @@ -38,14 +38,12 @@
     4.4      SDL_Window *window;
     4.5      SDL_Surface *surface;
     4.6  
     4.7 -    int windowed_width;
     4.8 -    int windowed_height;
     4.9 -
    4.10      float pixel_ratio;
    4.11  
    4.12      SDL_bool external_size;
    4.13  
    4.14      int requested_fullscreen_mode;
    4.15 +    SDL_bool fullscreen_resize;
    4.16  
    4.17      SDL_bool finger_touching;  /* for mapping touch events to mice */
    4.18      SDL_FingerID first_finger;