src/video/emscripten/SDL_emscriptenvideo.c
author Boris Gjenero <boris.gjenero@gmail.com>
Tue, 13 Sep 2016 00:03:53 -0700
changeset 10331 710c2ae96681
parent 10317 4a45690d5d82
child 10332 359290a75710
permissions -rw-r--r--
Support SDL_SetWindowTitle() via Module['setWindowTitle']()
     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_sysvideo.h"
    28 #include "../SDL_pixels_c.h"
    29 #include "../SDL_egl_c.h"
    30 #include "../../events/SDL_events_c.h"
    31 
    32 #include "SDL_emscriptenvideo.h"
    33 #include "SDL_emscriptenopengles.h"
    34 #include "SDL_emscriptenframebuffer.h"
    35 #include "SDL_emscriptenevents.h"
    36 #include "SDL_emscriptenmouse.h"
    37 
    38 #define EMSCRIPTENVID_DRIVER_NAME "emscripten"
    39 
    40 /* Initialization/Query functions */
    41 static int Emscripten_VideoInit(_THIS);
    42 static int Emscripten_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
    43 static void Emscripten_VideoQuit(_THIS);
    44 
    45 static int Emscripten_CreateWindow(_THIS, SDL_Window * window);
    46 static void Emscripten_SetWindowSize(_THIS, SDL_Window * window);
    47 static void Emscripten_DestroyWindow(_THIS, SDL_Window * window);
    48 static void Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
    49 static void Emscripten_PumpEvents(_THIS);
    50 static void Emscripten_SetWindowTitle(_THIS, SDL_Window * window);
    51 
    52 
    53 /* Emscripten driver bootstrap functions */
    54 
    55 static int
    56 Emscripten_Available(void)
    57 {
    58     return (1);
    59 }
    60 
    61 static void
    62 Emscripten_DeleteDevice(SDL_VideoDevice * device)
    63 {
    64     SDL_free(device);
    65 }
    66 
    67 static SDL_VideoDevice *
    68 Emscripten_CreateDevice(int devindex)
    69 {
    70     SDL_VideoDevice *device;
    71 
    72     /* Initialize all variables that we clean on shutdown */
    73     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    74     if (!device) {
    75         SDL_OutOfMemory();
    76         return (0);
    77     }
    78 
    79     /* Set the function pointers */
    80     device->VideoInit = Emscripten_VideoInit;
    81     device->VideoQuit = Emscripten_VideoQuit;
    82     device->SetDisplayMode = Emscripten_SetDisplayMode;
    83 
    84 
    85     device->PumpEvents = Emscripten_PumpEvents;
    86 
    87     device->CreateWindow = Emscripten_CreateWindow;
    88     /*device->CreateWindowFrom = Emscripten_CreateWindowFrom;*/
    89     device->SetWindowTitle = Emscripten_SetWindowTitle;
    90     /*device->SetWindowIcon = Emscripten_SetWindowIcon;
    91     device->SetWindowPosition = Emscripten_SetWindowPosition;*/
    92     device->SetWindowSize = Emscripten_SetWindowSize;
    93     /*device->ShowWindow = Emscripten_ShowWindow;
    94     device->HideWindow = Emscripten_HideWindow;
    95     device->RaiseWindow = Emscripten_RaiseWindow;
    96     device->MaximizeWindow = Emscripten_MaximizeWindow;
    97     device->MinimizeWindow = Emscripten_MinimizeWindow;
    98     device->RestoreWindow = Emscripten_RestoreWindow;
    99     device->SetWindowGrab = Emscripten_SetWindowGrab;*/
   100     device->DestroyWindow = Emscripten_DestroyWindow;
   101     device->SetWindowFullscreen = Emscripten_SetWindowFullscreen;
   102 
   103     device->CreateWindowFramebuffer = Emscripten_CreateWindowFramebuffer;
   104     device->UpdateWindowFramebuffer = Emscripten_UpdateWindowFramebuffer;
   105     device->DestroyWindowFramebuffer = Emscripten_DestroyWindowFramebuffer;
   106 
   107     device->GL_LoadLibrary = Emscripten_GLES_LoadLibrary;
   108     device->GL_GetProcAddress = Emscripten_GLES_GetProcAddress;
   109     device->GL_UnloadLibrary = Emscripten_GLES_UnloadLibrary;
   110     device->GL_CreateContext = Emscripten_GLES_CreateContext;
   111     device->GL_MakeCurrent = Emscripten_GLES_MakeCurrent;
   112     device->GL_SetSwapInterval = Emscripten_GLES_SetSwapInterval;
   113     device->GL_GetSwapInterval = Emscripten_GLES_GetSwapInterval;
   114     device->GL_SwapWindow = Emscripten_GLES_SwapWindow;
   115     device->GL_DeleteContext = Emscripten_GLES_DeleteContext;
   116     device->GL_GetDrawableSize = Emscripten_GLES_GetDrawableSize;
   117 
   118     device->free = Emscripten_DeleteDevice;
   119 
   120     return device;
   121 }
   122 
   123 VideoBootStrap Emscripten_bootstrap = {
   124     EMSCRIPTENVID_DRIVER_NAME, "SDL emscripten video driver",
   125     Emscripten_Available, Emscripten_CreateDevice
   126 };
   127 
   128 
   129 int
   130 Emscripten_VideoInit(_THIS)
   131 {
   132     SDL_DisplayMode mode;
   133     double css_w, css_h;
   134 
   135     /* Use a fake 32-bpp desktop mode */
   136     mode.format = SDL_PIXELFORMAT_RGB888;
   137 
   138     emscripten_get_element_css_size(NULL, &css_w, &css_h);
   139 
   140     mode.w = css_w;
   141     mode.h = css_h;
   142 
   143     mode.refresh_rate = 0;
   144     mode.driverdata = NULL;
   145     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   146         return -1;
   147     }
   148 
   149     SDL_zero(mode);
   150     SDL_AddDisplayMode(&_this->displays[0], &mode);
   151 
   152     Emscripten_InitMouse();
   153 
   154     /* We're done! */
   155     return 0;
   156 }
   157 
   158 static int
   159 Emscripten_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   160 {
   161     /* can't do this */
   162     return 0;
   163 }
   164 
   165 static void
   166 Emscripten_VideoQuit(_THIS)
   167 {
   168     Emscripten_FiniMouse();
   169 }
   170 
   171 static void
   172 Emscripten_PumpEvents(_THIS)
   173 {
   174     /* do nothing. */
   175 }
   176 
   177 static int
   178 Emscripten_CreateWindow(_THIS, SDL_Window * window)
   179 {
   180     SDL_WindowData *wdata;
   181     double scaled_w, scaled_h;
   182     double css_w, css_h;
   183 
   184     /* Allocate window internal data */
   185     wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
   186     if (wdata == NULL) {
   187         return SDL_OutOfMemory();
   188     }
   189 
   190     if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   191         wdata->pixel_ratio = emscripten_get_device_pixel_ratio();
   192     } else {
   193         wdata->pixel_ratio = 1.0f;
   194     }
   195 
   196     scaled_w = SDL_floor(window->w * wdata->pixel_ratio);
   197     scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
   198 
   199     emscripten_set_canvas_size(scaled_w, scaled_h);
   200 
   201     emscripten_get_element_css_size(NULL, &css_w, &css_h);
   202 
   203     wdata->external_size = css_w != scaled_w || css_h != scaled_h;
   204 
   205     if ((window->flags & SDL_WINDOW_RESIZABLE) && wdata->external_size) {
   206         /* external css has resized us */
   207         scaled_w = css_w * wdata->pixel_ratio;
   208         scaled_h = css_h * wdata->pixel_ratio;
   209 
   210         emscripten_set_canvas_size(scaled_w, scaled_h);
   211         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
   212     }
   213 
   214     /* if the size is not being controlled by css, we need to scale down for hidpi */
   215     if (!wdata->external_size) {
   216         if (wdata->pixel_ratio != 1.0f) {
   217             /*scale canvas down*/
   218             emscripten_set_element_css_size(NULL, window->w, window->h);
   219         }
   220     }
   221 
   222     if (window->flags & SDL_WINDOW_OPENGL) {
   223         if (!_this->egl_data) {
   224             if (SDL_GL_LoadLibrary(NULL) < 0) {
   225                 return -1;
   226             }
   227         }
   228         wdata->egl_surface = SDL_EGL_CreateSurface(_this, 0);
   229 
   230         if (wdata->egl_surface == EGL_NO_SURFACE) {
   231             return SDL_SetError("Could not create GLES window surface");
   232         }
   233     }
   234 
   235     wdata->window = window;
   236 
   237     /* Setup driver data for this window */
   238     window->driverdata = wdata;
   239 
   240     /* One window, it always has focus */
   241     SDL_SetMouseFocus(window);
   242     SDL_SetKeyboardFocus(window);
   243 
   244     Emscripten_RegisterEventHandlers(wdata);
   245 
   246     /* Window has been successfully created */
   247     return 0;
   248 }
   249 
   250 static void Emscripten_SetWindowSize(_THIS, SDL_Window * window)
   251 {
   252     SDL_WindowData *data;
   253 
   254     if (window->driverdata) {
   255         data = (SDL_WindowData *) window->driverdata;
   256         emscripten_set_canvas_size(window->w * data->pixel_ratio, window->h * data->pixel_ratio);
   257 
   258         /*scale canvas down*/
   259         if (!data->external_size && data->pixel_ratio != 1.0f) {
   260             emscripten_set_element_css_size(NULL, window->w, window->h);
   261         }
   262     }
   263 }
   264 
   265 static void
   266 Emscripten_DestroyWindow(_THIS, SDL_Window * window)
   267 {
   268     SDL_WindowData *data;
   269 
   270     if(window->driverdata) {
   271         data = (SDL_WindowData *) window->driverdata;
   272 
   273         Emscripten_UnregisterEventHandlers(data);
   274         if (data->egl_surface != EGL_NO_SURFACE) {
   275             SDL_EGL_DestroySurface(_this, data->egl_surface);
   276             data->egl_surface = EGL_NO_SURFACE;
   277         }
   278         SDL_free(window->driverdata);
   279         window->driverdata = NULL;
   280     }
   281 }
   282 
   283 static void
   284 Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   285 {
   286     SDL_WindowData *data;
   287     if(window->driverdata) {
   288         data = (SDL_WindowData *) window->driverdata;
   289 
   290         if(fullscreen) {
   291             EmscriptenFullscreenStrategy strategy;
   292             SDL_bool is_desktop_fullscreen = (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
   293 
   294             strategy.scaleMode = is_desktop_fullscreen ? EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH : EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT;
   295 
   296             if(!is_desktop_fullscreen) {
   297                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE;
   298             } else if(window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
   299                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_HIDEF;
   300             } else {
   301                 strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
   302             }
   303 
   304             strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
   305 
   306             strategy.canvasResizedCallback = Emscripten_HandleCanvasResize;
   307             strategy.canvasResizedCallbackUserData = data;
   308 
   309             data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
   310             data->fullscreen_resize = is_desktop_fullscreen;
   311             /*unset the fullscreen flags as we're not actually fullscreen yet*/
   312             window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
   313 
   314             emscripten_request_fullscreen_strategy(NULL, 1, &strategy);
   315         }
   316         else
   317             emscripten_exit_fullscreen();
   318     }
   319 }
   320 
   321 static void
   322 Emscripten_SetWindowTitle(_THIS, SDL_Window * window) {
   323     EM_ASM_INT({
   324       if (typeof Module['setWindowTitle'] !== 'undefined') {
   325         Module['setWindowTitle'](Module['Pointer_stringify']($0));
   326       }
   327       return 0;
   328     }, window->title);
   329 }
   330 
   331 #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
   332 
   333 /* vi: set ts=4 sw=4 expandtab: */