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