src/video/emscripten/SDL_emscriptenvideo.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 05 Aug 2017 22:10:48 +0200
changeset 11191 c3b94a174d81
parent 10895 fd63ed9b0746
child 11383 6cafb5954aaf
permissions -rw-r--r--
emscripten: Fixed compiling without OpenGL support.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 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      * when the user clicks to allow full screen.
    82      * See https://bugzilla.mozilla.org/show_bug.cgi?id=1144964
    83     */
    84     SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
    85 
    86     /* Set the function pointers */
    87     device->VideoInit = Emscripten_VideoInit;
    88     device->VideoQuit = Emscripten_VideoQuit;
    89     device->SetDisplayMode = Emscripten_SetDisplayMode;
    90 
    91 
    92     device->PumpEvents = Emscripten_PumpEvents;
    93 
    94     device->CreateWindow = Emscripten_CreateWindow;
    95     /*device->CreateWindowFrom = Emscripten_CreateWindowFrom;*/
    96     device->SetWindowTitle = Emscripten_SetWindowTitle;
    97     /*device->SetWindowIcon = Emscripten_SetWindowIcon;
    98     device->SetWindowPosition = Emscripten_SetWindowPosition;*/
    99     device->SetWindowSize = Emscripten_SetWindowSize;
   100     /*device->ShowWindow = Emscripten_ShowWindow;
   101     device->HideWindow = Emscripten_HideWindow;
   102     device->RaiseWindow = Emscripten_RaiseWindow;
   103     device->MaximizeWindow = Emscripten_MaximizeWindow;
   104     device->MinimizeWindow = Emscripten_MinimizeWindow;
   105     device->RestoreWindow = Emscripten_RestoreWindow;
   106     device->SetWindowGrab = Emscripten_SetWindowGrab;*/
   107     device->DestroyWindow = Emscripten_DestroyWindow;
   108     device->SetWindowFullscreen = Emscripten_SetWindowFullscreen;
   109 
   110     device->CreateWindowFramebuffer = Emscripten_CreateWindowFramebuffer;
   111     device->UpdateWindowFramebuffer = Emscripten_UpdateWindowFramebuffer;
   112     device->DestroyWindowFramebuffer = Emscripten_DestroyWindowFramebuffer;
   113 
   114 #if SDL_VIDEO_OPENGL_EGL
   115     device->GL_LoadLibrary = Emscripten_GLES_LoadLibrary;
   116     device->GL_GetProcAddress = Emscripten_GLES_GetProcAddress;
   117     device->GL_UnloadLibrary = Emscripten_GLES_UnloadLibrary;
   118     device->GL_CreateContext = Emscripten_GLES_CreateContext;
   119     device->GL_MakeCurrent = Emscripten_GLES_MakeCurrent;
   120     device->GL_SetSwapInterval = Emscripten_GLES_SetSwapInterval;
   121     device->GL_GetSwapInterval = Emscripten_GLES_GetSwapInterval;
   122     device->GL_SwapWindow = Emscripten_GLES_SwapWindow;
   123     device->GL_DeleteContext = Emscripten_GLES_DeleteContext;
   124     device->GL_GetDrawableSize = Emscripten_GLES_GetDrawableSize;
   125 #endif
   126 
   127     device->free = Emscripten_DeleteDevice;
   128 
   129     return device;
   130 }
   131 
   132 VideoBootStrap Emscripten_bootstrap = {
   133     EMSCRIPTENVID_DRIVER_NAME, "SDL emscripten video driver",
   134     Emscripten_Available, Emscripten_CreateDevice
   135 };
   136 
   137 
   138 int
   139 Emscripten_VideoInit(_THIS)
   140 {
   141     SDL_DisplayMode mode;
   142 
   143     /* Use a fake 32-bpp desktop mode */
   144     mode.format = SDL_PIXELFORMAT_RGB888;
   145 
   146     mode.w = EM_ASM_INT_V({
   147         return screen.width;
   148     });
   149 
   150     mode.h = EM_ASM_INT_V({
   151         return screen.height;
   152     });
   153 
   154     mode.refresh_rate = 0;
   155     mode.driverdata = NULL;
   156     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   157         return -1;
   158     }
   159 
   160     SDL_zero(mode);
   161     SDL_AddDisplayMode(&_this->displays[0], &mode);
   162 
   163     Emscripten_InitMouse();
   164 
   165     /* We're done! */
   166     return 0;
   167 }
   168 
   169 static int
   170 Emscripten_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   171 {
   172     /* can't do this */
   173     return 0;
   174 }
   175 
   176 static void
   177 Emscripten_VideoQuit(_THIS)
   178 {
   179     Emscripten_FiniMouse();
   180 }
   181 
   182 static void
   183 Emscripten_PumpEvents(_THIS)
   184 {
   185     /* do nothing. */
   186 }
   187 
   188 static int
   189 Emscripten_CreateWindow(_THIS, SDL_Window * window)
   190 {
   191     SDL_WindowData *wdata;
   192     double scaled_w, scaled_h;
   193     double css_w, css_h;
   194 
   195     /* Allocate window internal data */
   196     wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
   197     if (wdata == NULL) {
   198         return SDL_OutOfMemory();
   199     }
   200 
   201     if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   202         wdata->pixel_ratio = emscripten_get_device_pixel_ratio();
   203     } else {
   204         wdata->pixel_ratio = 1.0f;
   205     }
   206 
   207     scaled_w = SDL_floor(window->w * wdata->pixel_ratio);
   208     scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
   209 
   210     emscripten_set_canvas_size(scaled_w, scaled_h);
   211 
   212     emscripten_get_element_css_size(NULL, &css_w, &css_h);
   213 
   214     wdata->external_size = SDL_floor(css_w) != scaled_w || SDL_floor(css_h) != scaled_h;
   215 
   216     if ((window->flags & SDL_WINDOW_RESIZABLE) && wdata->external_size) {
   217         /* external css has resized us */
   218         scaled_w = css_w * wdata->pixel_ratio;
   219         scaled_h = css_h * wdata->pixel_ratio;
   220 
   221         emscripten_set_canvas_size(scaled_w, scaled_h);
   222         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
   223     }
   224 
   225     /* if the size is not being controlled by css, we need to scale down for hidpi */
   226     if (!wdata->external_size) {
   227         if (wdata->pixel_ratio != 1.0f) {
   228             /*scale canvas down*/
   229             emscripten_set_element_css_size(NULL, window->w, window->h);
   230         }
   231     }
   232 
   233 #if SDL_VIDEO_OPENGL_EGL
   234     if (window->flags & SDL_WINDOW_OPENGL) {
   235         if (!_this->egl_data) {
   236             if (SDL_GL_LoadLibrary(NULL) < 0) {
   237                 return -1;
   238             }
   239         }
   240         wdata->egl_surface = SDL_EGL_CreateSurface(_this, 0);
   241 
   242         if (wdata->egl_surface == EGL_NO_SURFACE) {
   243             return SDL_SetError("Could not create GLES window surface");
   244         }
   245     }
   246 #endif
   247 
   248     wdata->window = window;
   249 
   250     /* Setup driver data for this window */
   251     window->driverdata = wdata;
   252 
   253     /* One window, it always has focus */
   254     SDL_SetMouseFocus(window);
   255     SDL_SetKeyboardFocus(window);
   256 
   257     Emscripten_RegisterEventHandlers(wdata);
   258 
   259     /* Window has been successfully created */
   260     return 0;
   261 }
   262 
   263 static void Emscripten_SetWindowSize(_THIS, SDL_Window * window)
   264 {
   265     SDL_WindowData *data;
   266 
   267     if (window->driverdata) {
   268         data = (SDL_WindowData *) window->driverdata;
   269         /* update pixel ratio */
   270         if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   271             data->pixel_ratio = emscripten_get_device_pixel_ratio();
   272         }
   273         emscripten_set_canvas_size(window->w * data->pixel_ratio, window->h * data->pixel_ratio);
   274 
   275         /*scale canvas down*/
   276         if (!data->external_size && data->pixel_ratio != 1.0f) {
   277             emscripten_set_element_css_size(NULL, window->w, window->h);
   278         }
   279     }
   280 }
   281 
   282 static void
   283 Emscripten_DestroyWindow(_THIS, SDL_Window * window)
   284 {
   285     SDL_WindowData *data;
   286 
   287     if(window->driverdata) {
   288         data = (SDL_WindowData *) window->driverdata;
   289 
   290         Emscripten_UnregisterEventHandlers(data);
   291 #if SDL_VIDEO_OPENGL_EGL
   292         if (data->egl_surface != EGL_NO_SURFACE) {
   293             SDL_EGL_DestroySurface(_this, data->egl_surface);
   294             data->egl_surface = EGL_NO_SURFACE;
   295         }
   296 #endif
   297         SDL_free(window->driverdata);
   298         window->driverdata = NULL;
   299     }
   300 }
   301 
   302 static void
   303 Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   304 {
   305     SDL_WindowData *data;
   306     if(window->driverdata) {
   307         data = (SDL_WindowData *) window->driverdata;
   308 
   309         if(fullscreen) {
   310             EmscriptenFullscreenStrategy strategy;
   311             SDL_bool is_desktop_fullscreen = (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
   312             int res;
   313 
   314             strategy.scaleMode = is_desktop_fullscreen ? EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH : EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT;
   315 
   316             if(!is_desktop_fullscreen) {
   317                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE;
   318             } else if(window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   319                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_HIDEF;
   320             } else {
   321                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
   322             }
   323 
   324             strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
   325 
   326             strategy.canvasResizedCallback = Emscripten_HandleCanvasResize;
   327             strategy.canvasResizedCallbackUserData = data;
   328 
   329             data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
   330             data->fullscreen_resize = is_desktop_fullscreen;
   331 
   332             res = emscripten_request_fullscreen_strategy(NULL, 1, &strategy);
   333             if(res != EMSCRIPTEN_RESULT_SUCCESS && res != EMSCRIPTEN_RESULT_DEFERRED) {
   334                 /* unset flags, fullscreen failed */
   335                 window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
   336             }
   337         }
   338         else
   339             emscripten_exit_fullscreen();
   340     }
   341 }
   342 
   343 static void
   344 Emscripten_SetWindowTitle(_THIS, SDL_Window * window) {
   345     EM_ASM_INT({
   346       if (typeof Module['setWindowTitle'] !== 'undefined') {
   347         Module['setWindowTitle'](Module['Pointer_stringify']($0));
   348       }
   349       return 0;
   350     }, window->title);
   351 }
   352 
   353 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
   354 
   355 /* vi: set ts=4 sw=4 expandtab: */