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