SDL_emscriptenvideo.c 12 KB

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