src/video/emscripten/SDL_emscriptenvideo.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 18 Dec 2014 00:19:52 -0500
changeset 9278 8900afb78a19
child 9294 1865bd71ef12
permissions -rw-r--r--
Initial merge of Emscripten port!

With this commit, you can compile SDL2 with Emscripten
( http://emscripten.org/ ), and make your SDL-based C/C++ program
into a web app.

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