src/video/emscripten/SDL_emscriptenvideo.c
author Charlie Birks
Tue, 13 Sep 2016 00:03:56 -0700
changeset 10334 0494a8336c48
parent 10333 a0495857c42f
child 10335 77b7fccaaa01
permissions -rw-r--r--
only unset fullscreen flags if fullscreen failed
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_EMSCRIPTEN
    24 
    25 #include "SDL_video.h"
    26 #include "SDL_mouse.h"
    27 #include "SDL_hints.h"
    28 #include "../SDL_sysvideo.h"
    29 #include "../SDL_pixels_c.h"
    30 #include "../SDL_egl_c.h"
    31 #include "../../events/SDL_events_c.h"
    32 
    33 #include "SDL_emscriptenvideo.h"
    34 #include "SDL_emscriptenopengles.h"
    35 #include "SDL_emscriptenframebuffer.h"
    36 #include "SDL_emscriptenevents.h"
    37 #include "SDL_emscriptenmouse.h"
    38 
    39 #define EMSCRIPTENVID_DRIVER_NAME "emscripten"
    40 
    41 /* Initialization/Query functions */
    42 static int Emscripten_VideoInit(_THIS);
    43 static int Emscripten_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
    44 static void Emscripten_VideoQuit(_THIS);
    45 
    46 static int Emscripten_CreateWindow(_THIS, SDL_Window * window);
    47 static void Emscripten_SetWindowSize(_THIS, SDL_Window * window);
    48 static void Emscripten_DestroyWindow(_THIS, SDL_Window * window);
    49 static void Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
    50 static void Emscripten_PumpEvents(_THIS);
    51 static void Emscripten_SetWindowTitle(_THIS, SDL_Window * window);
    52 
    53 
    54 /* Emscripten driver bootstrap functions */
    55 
    56 static int
    57 Emscripten_Available(void)
    58 {
    59     return (1);
    60 }
    61 
    62 static void
    63 Emscripten_DeleteDevice(SDL_VideoDevice * device)
    64 {
    65     SDL_free(device);
    66 }
    67 
    68 static SDL_VideoDevice *
    69 Emscripten_CreateDevice(int devindex)
    70 {
    71     SDL_VideoDevice *device;
    72 
    73     /* Initialize all variables that we clean on shutdown */
    74     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    75     if (!device) {
    76         SDL_OutOfMemory();
    77         return (0);
    78     }
    79 
    80     /* Firefox sends blur event which would otherwise prevent full screen */
    81     SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
    82 
    83     /* Set the function pointers */
    84     device->VideoInit = Emscripten_VideoInit;
    85     device->VideoQuit = Emscripten_VideoQuit;
    86     device->SetDisplayMode = Emscripten_SetDisplayMode;
    87 
    88 
    89     device->PumpEvents = Emscripten_PumpEvents;
    90 
    91     device->CreateWindow = Emscripten_CreateWindow;
    92     /*device->CreateWindowFrom = Emscripten_CreateWindowFrom;*/
    93     device->SetWindowTitle = Emscripten_SetWindowTitle;
    94     /*device->SetWindowIcon = Emscripten_SetWindowIcon;
    95     device->SetWindowPosition = Emscripten_SetWindowPosition;*/
    96     device->SetWindowSize = Emscripten_SetWindowSize;
    97     /*device->ShowWindow = Emscripten_ShowWindow;
    98     device->HideWindow = Emscripten_HideWindow;
    99     device->RaiseWindow = Emscripten_RaiseWindow;
   100     device->MaximizeWindow = Emscripten_MaximizeWindow;
   101     device->MinimizeWindow = Emscripten_MinimizeWindow;
   102     device->RestoreWindow = Emscripten_RestoreWindow;
   103     device->SetWindowGrab = Emscripten_SetWindowGrab;*/
   104     device->DestroyWindow = Emscripten_DestroyWindow;
   105     device->SetWindowFullscreen = Emscripten_SetWindowFullscreen;
   106 
   107     device->CreateWindowFramebuffer = Emscripten_CreateWindowFramebuffer;
   108     device->UpdateWindowFramebuffer = Emscripten_UpdateWindowFramebuffer;
   109     device->DestroyWindowFramebuffer = Emscripten_DestroyWindowFramebuffer;
   110 
   111     device->GL_LoadLibrary = Emscripten_GLES_LoadLibrary;
   112     device->GL_GetProcAddress = Emscripten_GLES_GetProcAddress;
   113     device->GL_UnloadLibrary = Emscripten_GLES_UnloadLibrary;
   114     device->GL_CreateContext = Emscripten_GLES_CreateContext;
   115     device->GL_MakeCurrent = Emscripten_GLES_MakeCurrent;
   116     device->GL_SetSwapInterval = Emscripten_GLES_SetSwapInterval;
   117     device->GL_GetSwapInterval = Emscripten_GLES_GetSwapInterval;
   118     device->GL_SwapWindow = Emscripten_GLES_SwapWindow;
   119     device->GL_DeleteContext = Emscripten_GLES_DeleteContext;
   120     device->GL_GetDrawableSize = Emscripten_GLES_GetDrawableSize;
   121 
   122     device->free = Emscripten_DeleteDevice;
   123 
   124     return device;
   125 }
   126 
   127 VideoBootStrap Emscripten_bootstrap = {
   128     EMSCRIPTENVID_DRIVER_NAME, "SDL emscripten video driver",
   129     Emscripten_Available, Emscripten_CreateDevice
   130 };
   131 
   132 
   133 int
   134 Emscripten_VideoInit(_THIS)
   135 {
   136     SDL_DisplayMode mode;
   137 
   138     /* Use a fake 32-bpp desktop mode */
   139     mode.format = SDL_PIXELFORMAT_RGB888;
   140 
   141     mode.w = EM_ASM_INT_V({
   142         return screen.width;
   143     });
   144 
   145     mode.h = EM_ASM_INT_V({
   146         return screen.height;
   147     });
   148 
   149     mode.refresh_rate = 0;
   150     mode.driverdata = NULL;
   151     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   152         return -1;
   153     }
   154 
   155     SDL_zero(mode);
   156     SDL_AddDisplayMode(&_this->displays[0], &mode);
   157 
   158     Emscripten_InitMouse();
   159 
   160     /* We're done! */
   161     return 0;
   162 }
   163 
   164 static int
   165 Emscripten_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   166 {
   167     /* can't do this */
   168     return 0;
   169 }
   170 
   171 static void
   172 Emscripten_VideoQuit(_THIS)
   173 {
   174     Emscripten_FiniMouse();
   175 }
   176 
   177 static void
   178 Emscripten_PumpEvents(_THIS)
   179 {
   180     /* do nothing. */
   181 }
   182 
   183 static int
   184 Emscripten_CreateWindow(_THIS, SDL_Window * window)
   185 {
   186     SDL_WindowData *wdata;
   187     double scaled_w, scaled_h;
   188     double css_w, css_h;
   189 
   190     /* Allocate window internal data */
   191     wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
   192     if (wdata == NULL) {
   193         return SDL_OutOfMemory();
   194     }
   195 
   196     if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   197         wdata->pixel_ratio = emscripten_get_device_pixel_ratio();
   198     } else {
   199         wdata->pixel_ratio = 1.0f;
   200     }
   201 
   202     scaled_w = SDL_floor(window->w * wdata->pixel_ratio);
   203     scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
   204 
   205     emscripten_set_canvas_size(scaled_w, scaled_h);
   206 
   207     emscripten_get_element_css_size(NULL, &css_w, &css_h);
   208 
   209     wdata->external_size = css_w != scaled_w || css_h != scaled_h;
   210 
   211     if ((window->flags & SDL_WINDOW_RESIZABLE) && wdata->external_size) {
   212         /* external css has resized us */
   213         scaled_w = css_w * wdata->pixel_ratio;
   214         scaled_h = css_h * wdata->pixel_ratio;
   215 
   216         emscripten_set_canvas_size(scaled_w, scaled_h);
   217         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
   218     }
   219 
   220     /* if the size is not being controlled by css, we need to scale down for hidpi */
   221     if (!wdata->external_size) {
   222         if (wdata->pixel_ratio != 1.0f) {
   223             /*scale canvas down*/
   224             emscripten_set_element_css_size(NULL, window->w, window->h);
   225         }
   226     }
   227 
   228     if (window->flags & SDL_WINDOW_OPENGL) {
   229         if (!_this->egl_data) {
   230             if (SDL_GL_LoadLibrary(NULL) < 0) {
   231                 return -1;
   232             }
   233         }
   234         wdata->egl_surface = SDL_EGL_CreateSurface(_this, 0);
   235 
   236         if (wdata->egl_surface == EGL_NO_SURFACE) {
   237             return SDL_SetError("Could not create GLES window surface");
   238         }
   239     }
   240 
   241     wdata->window = window;
   242 
   243     /* Setup driver data for this window */
   244     window->driverdata = wdata;
   245 
   246     /* One window, it always has focus */
   247     SDL_SetMouseFocus(window);
   248     SDL_SetKeyboardFocus(window);
   249 
   250     Emscripten_RegisterEventHandlers(wdata);
   251 
   252     /* Window has been successfully created */
   253     return 0;
   254 }
   255 
   256 static void Emscripten_SetWindowSize(_THIS, SDL_Window * window)
   257 {
   258     SDL_WindowData *data;
   259 
   260     if (window->driverdata) {
   261         data = (SDL_WindowData *) window->driverdata;
   262         emscripten_set_canvas_size(window->w * data->pixel_ratio, window->h * data->pixel_ratio);
   263 
   264         /*scale canvas down*/
   265         if (!data->external_size && data->pixel_ratio != 1.0f) {
   266             emscripten_set_element_css_size(NULL, window->w, window->h);
   267         }
   268     }
   269 }
   270 
   271 static void
   272 Emscripten_DestroyWindow(_THIS, SDL_Window * window)
   273 {
   274     SDL_WindowData *data;
   275 
   276     if(window->driverdata) {
   277         data = (SDL_WindowData *) window->driverdata;
   278 
   279         Emscripten_UnregisterEventHandlers(data);
   280         if (data->egl_surface != EGL_NO_SURFACE) {
   281             SDL_EGL_DestroySurface(_this, data->egl_surface);
   282             data->egl_surface = EGL_NO_SURFACE;
   283         }
   284         SDL_free(window->driverdata);
   285         window->driverdata = NULL;
   286     }
   287 }
   288 
   289 static void
   290 Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   291 {
   292     SDL_WindowData *data;
   293     if(window->driverdata) {
   294         data = (SDL_WindowData *) window->driverdata;
   295 
   296         if(fullscreen) {
   297             EmscriptenFullscreenStrategy strategy;
   298             SDL_bool is_desktop_fullscreen = (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
   299             int res;
   300 
   301             strategy.scaleMode = is_desktop_fullscreen ? EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH : EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT;
   302 
   303             if(!is_desktop_fullscreen) {
   304                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE;
   305             } else if(window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   306                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_HIDEF;
   307             } else {
   308                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
   309             }
   310 
   311             strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
   312 
   313             strategy.canvasResizedCallback = Emscripten_HandleCanvasResize;
   314             strategy.canvasResizedCallbackUserData = data;
   315 
   316             data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
   317             data->fullscreen_resize = is_desktop_fullscreen;
   318 
   319             res = emscripten_request_fullscreen_strategy(NULL, 1, &strategy);
   320             if(res != EMSCRIPTEN_RESULT_SUCCESS && res != EMSCRIPTEN_RESULT_DEFERRED) {
   321                 /* unset flags, fullscreen failed */
   322                 window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
   323             }
   324         }
   325         else
   326             emscripten_exit_fullscreen();
   327     }
   328 }
   329 
   330 static void
   331 Emscripten_SetWindowTitle(_THIS, SDL_Window * window) {
   332     EM_ASM_INT({
   333       if (typeof Module['setWindowTitle'] !== 'undefined') {
   334         Module['setWindowTitle'](Module['Pointer_stringify']($0));
   335       }
   336       return 0;
   337     }, window->title);
   338 }
   339 
   340 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
   341 
   342 /* vi: set ts=4 sw=4 expandtab: */