1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/video/emscripten/SDL_emscriptenvideo.c Thu Dec 18 00:19:52 2014 -0500
1.3 @@ -0,0 +1,320 @@
1.4 +/*
1.5 + Simple DirectMedia Layer
1.6 + Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
1.7 +
1.8 + This software is provided 'as-is', without any express or implied
1.9 + warranty. In no event will the authors be held liable for any damages
1.10 + arising from the use of this software.
1.11 +
1.12 + Permission is granted to anyone to use this software for any purpose,
1.13 + including commercial applications, and to alter it and redistribute it
1.14 + freely, subject to the following restrictions:
1.15 +
1.16 + 1. The origin of this software must not be misrepresented; you must not
1.17 + claim that you wrote the original software. If you use this software
1.18 + in a product, an acknowledgment in the product documentation would be
1.19 + appreciated but is not required.
1.20 + 2. Altered source versions must be plainly marked as such, and must not be
1.21 + misrepresented as being the original software.
1.22 + 3. This notice may not be removed or altered from any source distribution.
1.23 +*/
1.24 +#include "../../SDL_internal.h"
1.25 +
1.26 +#if SDL_VIDEO_DRIVER_EMSCRIPTEN
1.27 +
1.28 +#include "SDL_video.h"
1.29 +#include "SDL_mouse.h"
1.30 +#include "../SDL_sysvideo.h"
1.31 +#include "../SDL_pixels_c.h"
1.32 +#include "../SDL_egl_c.h"
1.33 +#include "../../events/SDL_events_c.h"
1.34 +
1.35 +#include "SDL_emscriptenvideo.h"
1.36 +#include "SDL_emscriptenopengles.h"
1.37 +#include "SDL_emscriptenframebuffer.h"
1.38 +#include "SDL_emscriptenevents.h"
1.39 +#include "SDL_emscriptenmouse.h"
1.40 +
1.41 +#define EMSCRIPTENVID_DRIVER_NAME "emscripten"
1.42 +
1.43 +/* Initialization/Query functions */
1.44 +static int Emscripten_VideoInit(_THIS);
1.45 +static int Emscripten_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
1.46 +static void Emscripten_VideoQuit(_THIS);
1.47 +
1.48 +static int Emscripten_CreateWindow(_THIS, SDL_Window * window);
1.49 +static void Emscripten_SetWindowSize(_THIS, SDL_Window * window);
1.50 +static void Emscripten_DestroyWindow(_THIS, SDL_Window * window);
1.51 +static void Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
1.52 +static void Emscripten_PumpEvents(_THIS);
1.53 +
1.54 +
1.55 +/* Emscripten driver bootstrap functions */
1.56 +
1.57 +static int
1.58 +Emscripten_Available(void)
1.59 +{
1.60 + return (1);
1.61 +}
1.62 +
1.63 +static void
1.64 +Emscripten_DeleteDevice(SDL_VideoDevice * device)
1.65 +{
1.66 + SDL_free(device);
1.67 +}
1.68 +
1.69 +static SDL_VideoDevice *
1.70 +Emscripten_CreateDevice(int devindex)
1.71 +{
1.72 + SDL_VideoDevice *device;
1.73 +
1.74 + /* Initialize all variables that we clean on shutdown */
1.75 + device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
1.76 + if (!device) {
1.77 + SDL_OutOfMemory();
1.78 + SDL_free(device);
1.79 + return (0);
1.80 + }
1.81 +
1.82 + /* Set the function pointers */
1.83 + device->VideoInit = Emscripten_VideoInit;
1.84 + device->VideoQuit = Emscripten_VideoQuit;
1.85 + device->SetDisplayMode = Emscripten_SetDisplayMode;
1.86 +
1.87 +
1.88 + device->PumpEvents = Emscripten_PumpEvents;
1.89 +
1.90 + device->CreateWindow = Emscripten_CreateWindow;
1.91 + /*device->CreateWindowFrom = Emscripten_CreateWindowFrom;
1.92 + device->SetWindowTitle = Emscripten_SetWindowTitle;
1.93 + device->SetWindowIcon = Emscripten_SetWindowIcon;
1.94 + device->SetWindowPosition = Emscripten_SetWindowPosition;*/
1.95 + device->SetWindowSize = Emscripten_SetWindowSize;
1.96 + /*device->ShowWindow = Emscripten_ShowWindow;
1.97 + device->HideWindow = Emscripten_HideWindow;
1.98 + device->RaiseWindow = Emscripten_RaiseWindow;
1.99 + device->MaximizeWindow = Emscripten_MaximizeWindow;
1.100 + device->MinimizeWindow = Emscripten_MinimizeWindow;
1.101 + device->RestoreWindow = Emscripten_RestoreWindow;
1.102 + device->SetWindowGrab = Emscripten_SetWindowGrab;*/
1.103 + device->DestroyWindow = Emscripten_DestroyWindow;
1.104 + device->SetWindowFullscreen = Emscripten_SetWindowFullscreen;
1.105 +
1.106 + device->CreateWindowFramebuffer = Emscripten_CreateWindowFramebuffer;
1.107 + device->UpdateWindowFramebuffer = Emscripten_UpdateWindowFramebuffer;
1.108 + device->DestroyWindowFramebuffer = Emscripten_DestroyWindowFramebuffer;
1.109 +
1.110 + device->GL_LoadLibrary = Emscripten_GLES_LoadLibrary;
1.111 + device->GL_GetProcAddress = Emscripten_GLES_GetProcAddress;
1.112 + device->GL_UnloadLibrary = Emscripten_GLES_UnloadLibrary;
1.113 + device->GL_CreateContext = Emscripten_GLES_CreateContext;
1.114 + device->GL_MakeCurrent = Emscripten_GLES_MakeCurrent;
1.115 + device->GL_SetSwapInterval = Emscripten_GLES_SetSwapInterval;
1.116 + device->GL_GetSwapInterval = Emscripten_GLES_GetSwapInterval;
1.117 + device->GL_SwapWindow = Emscripten_GLES_SwapWindow;
1.118 + device->GL_DeleteContext = Emscripten_GLES_DeleteContext;
1.119 + device->GL_GetDrawableSize = Emscripten_GLES_GetDrawableSize;
1.120 +
1.121 + device->free = Emscripten_DeleteDevice;
1.122 +
1.123 + return device;
1.124 +}
1.125 +
1.126 +VideoBootStrap Emscripten_bootstrap = {
1.127 + EMSCRIPTENVID_DRIVER_NAME, "SDL emscripten video driver",
1.128 + Emscripten_Available, Emscripten_CreateDevice
1.129 +};
1.130 +
1.131 +
1.132 +int
1.133 +Emscripten_VideoInit(_THIS)
1.134 +{
1.135 + SDL_DisplayMode mode;
1.136 + double css_w, css_h;
1.137 +
1.138 + /* Use a fake 32-bpp desktop mode */
1.139 + mode.format = SDL_PIXELFORMAT_RGB888;
1.140 +
1.141 + emscripten_get_element_css_size(NULL, &css_w, &css_h);
1.142 +
1.143 + mode.w = css_w;
1.144 + mode.h = css_h;
1.145 +
1.146 + mode.refresh_rate = 0;
1.147 + mode.driverdata = NULL;
1.148 + if (SDL_AddBasicVideoDisplay(&mode) < 0) {
1.149 + return -1;
1.150 + }
1.151 +
1.152 + SDL_zero(mode);
1.153 + SDL_AddDisplayMode(&_this->displays[0], &mode);
1.154 +
1.155 + Emscripten_InitMouse();
1.156 +
1.157 + /* We're done! */
1.158 + return 0;
1.159 +}
1.160 +
1.161 +static int
1.162 +Emscripten_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
1.163 +{
1.164 + /* can't do this */
1.165 + return 0;
1.166 +}
1.167 +
1.168 +static void
1.169 +Emscripten_VideoQuit(_THIS)
1.170 +{
1.171 + Emscripten_FiniMouse();
1.172 +}
1.173 +
1.174 +static void
1.175 +Emscripten_PumpEvents(_THIS)
1.176 +{
1.177 + /* do nothing. */
1.178 +}
1.179 +
1.180 +static int
1.181 +Emscripten_CreateWindow(_THIS, SDL_Window * window)
1.182 +{
1.183 + SDL_WindowData *wdata;
1.184 + double scaled_w, scaled_h;
1.185 + double css_w, css_h;
1.186 +
1.187 + /* Allocate window internal data */
1.188 + wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
1.189 + if (wdata == NULL) {
1.190 + return SDL_OutOfMemory();
1.191 + }
1.192 +
1.193 + if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
1.194 + wdata->pixel_ratio = emscripten_get_device_pixel_ratio();
1.195 + } else {
1.196 + wdata->pixel_ratio = 1.0f;
1.197 + }
1.198 +
1.199 + scaled_w = SDL_floor(window->w * wdata->pixel_ratio);
1.200 + scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
1.201 +
1.202 + emscripten_set_canvas_size(scaled_w, scaled_h);
1.203 +
1.204 + emscripten_get_element_css_size(NULL, &css_w, &css_h);
1.205 +
1.206 + wdata->external_size = css_w != scaled_w || css_h != scaled_h;
1.207 +
1.208 + if ((window->flags & SDL_WINDOW_RESIZABLE) && wdata->external_size) {
1.209 + /* external css has resized us */
1.210 + scaled_w = css_w * wdata->pixel_ratio;
1.211 + scaled_h = css_h * wdata->pixel_ratio;
1.212 +
1.213 + emscripten_set_canvas_size(scaled_w, scaled_h);
1.214 + SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
1.215 + }
1.216 +
1.217 + /* if the size is not being controlled by css, we need to scale down for hidpi */
1.218 + if (!wdata->external_size) {
1.219 + if (wdata->pixel_ratio != 1.0f) {
1.220 + /*scale canvas down*/
1.221 + emscripten_set_element_css_size(NULL, window->w, window->h);
1.222 + }
1.223 + }
1.224 +
1.225 + wdata->windowed_width = scaled_w;
1.226 + wdata->windowed_height = scaled_h;
1.227 +
1.228 + if (window->flags & SDL_WINDOW_OPENGL) {
1.229 + if (!_this->egl_data) {
1.230 + if (SDL_GL_LoadLibrary(NULL) < 0) {
1.231 + return -1;
1.232 + }
1.233 + }
1.234 + wdata->egl_surface = SDL_EGL_CreateSurface(_this, NULL);
1.235 +
1.236 + if (wdata->egl_surface == EGL_NO_SURFACE) {
1.237 + return SDL_SetError("Could not create GLES window surface");
1.238 + }
1.239 + }
1.240 +
1.241 + wdata->window = window;
1.242 +
1.243 + /* Setup driver data for this window */
1.244 + window->driverdata = wdata;
1.245 +
1.246 + /* One window, it always has focus */
1.247 + SDL_SetMouseFocus(window);
1.248 + SDL_SetKeyboardFocus(window);
1.249 +
1.250 + Emscripten_RegisterEventHandlers(wdata);
1.251 +
1.252 + /* Window has been successfully created */
1.253 + return 0;
1.254 +}
1.255 +
1.256 +static void Emscripten_SetWindowSize(_THIS, SDL_Window * window)
1.257 +{
1.258 + SDL_WindowData *data;
1.259 +
1.260 + if (window->driverdata) {
1.261 + data = (SDL_WindowData *) window->driverdata;
1.262 + emscripten_set_canvas_size(window->w * data->pixel_ratio, window->h * data->pixel_ratio);
1.263 +
1.264 + /*scale canvas down*/
1.265 + if (!data->external_size && data->pixel_ratio != 1.0f) {
1.266 + emscripten_set_element_css_size(NULL, window->w, window->h);
1.267 + }
1.268 + }
1.269 +}
1.270 +
1.271 +static void
1.272 +Emscripten_DestroyWindow(_THIS, SDL_Window * window)
1.273 +{
1.274 + SDL_WindowData *data;
1.275 +
1.276 + if(window->driverdata) {
1.277 + data = (SDL_WindowData *) window->driverdata;
1.278 +
1.279 + Emscripten_UnregisterEventHandlers(data);
1.280 + if (data->egl_surface != EGL_NO_SURFACE) {
1.281 + SDL_EGL_DestroySurface(_this, data->egl_surface);
1.282 + data->egl_surface = EGL_NO_SURFACE;
1.283 + }
1.284 + SDL_free(window->driverdata);
1.285 + window->driverdata = NULL;
1.286 + }
1.287 +}
1.288 +
1.289 +static void
1.290 +Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
1.291 +{
1.292 + SDL_WindowData *data;
1.293 + if(window->driverdata) {
1.294 + data = (SDL_WindowData *) window->driverdata;
1.295 +
1.296 + if(fullscreen) {
1.297 + data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
1.298 + /*unset the fullscreen flags as we're not actually fullscreen yet*/
1.299 + window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
1.300 +
1.301 + EM_ASM({
1.302 + //reparent canvas (similar to Module.requestFullscreen)
1.303 + var canvas = Module['canvas'];
1.304 + if(canvas.parentNode.id != "SDLFullscreenElement") {
1.305 + var canvasContainer = document.createElement("div");
1.306 + canvasContainer.id = "SDLFullscreenElement";
1.307 + canvas.parentNode.insertBefore(canvasContainer, canvas);
1.308 + canvasContainer.appendChild(canvas);
1.309 + }
1.310 + });
1.311 +
1.312 + int is_fullscreen;
1.313 + emscripten_get_canvas_size(&data->windowed_width, &data->windowed_height, &is_fullscreen);
1.314 + emscripten_request_fullscreen("SDLFullscreenElement", 1);
1.315 + }
1.316 + else
1.317 + emscripten_exit_fullscreen();
1.318 + }
1.319 +}
1.320 +
1.321 +#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
1.322 +
1.323 +/* vi: set ts=4 sw=4 expandtab: */