SDL_emscriptenvideo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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->SetWindowMouseGrab = Emscripten_SetWindowMouseGrab;*/
  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. emscripten_get_screen_size(&mode.w, &mode.h);
  114. mode.refresh_rate = 0;
  115. mode.driverdata = NULL;
  116. if (SDL_AddBasicVideoDisplay(&mode) < 0) {
  117. return -1;
  118. }
  119. SDL_AddDisplayMode(&_this->displays[0], &mode);
  120. Emscripten_InitMouse();
  121. /* We're done! */
  122. return 0;
  123. }
  124. static int
  125. Emscripten_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
  126. {
  127. /* can't do this */
  128. return 0;
  129. }
  130. static void
  131. Emscripten_VideoQuit(_THIS)
  132. {
  133. Emscripten_FiniMouse();
  134. }
  135. static int
  136. Emscripten_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect)
  137. {
  138. if (rect) {
  139. rect->x = 0;
  140. rect->y = 0;
  141. rect->w = EM_ASM_INT_V({
  142. return window.innerWidth;
  143. });
  144. rect->h = EM_ASM_INT_V({
  145. return window.innerHeight;
  146. });
  147. }
  148. return 0;
  149. }
  150. static void
  151. Emscripten_PumpEvents(_THIS)
  152. {
  153. /* do nothing. */
  154. }
  155. static int
  156. Emscripten_CreateWindow(_THIS, SDL_Window * window)
  157. {
  158. SDL_WindowData *wdata;
  159. double scaled_w, scaled_h;
  160. double css_w, css_h;
  161. /* Allocate window internal data */
  162. wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
  163. if (wdata == NULL) {
  164. return SDL_OutOfMemory();
  165. }
  166. wdata->canvas_id = SDL_strdup("#canvas");
  167. if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
  168. wdata->pixel_ratio = emscripten_get_device_pixel_ratio();
  169. } else {
  170. wdata->pixel_ratio = 1.0f;
  171. }
  172. scaled_w = SDL_floor(window->w * wdata->pixel_ratio);
  173. scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
  174. /* set a fake size to check if there is any CSS sizing the canvas */
  175. emscripten_set_canvas_element_size(wdata->canvas_id, 1, 1);
  176. emscripten_get_element_css_size(wdata->canvas_id, &css_w, &css_h);
  177. wdata->external_size = SDL_floor(css_w) != 1 || SDL_floor(css_h) != 1;
  178. if ((window->flags & SDL_WINDOW_RESIZABLE) && wdata->external_size) {
  179. /* external css has resized us */
  180. scaled_w = css_w * wdata->pixel_ratio;
  181. scaled_h = css_h * wdata->pixel_ratio;
  182. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, css_w, css_h);
  183. }
  184. emscripten_set_canvas_element_size(wdata->canvas_id, scaled_w, scaled_h);
  185. /* if the size is not being controlled by css, we need to scale down for hidpi */
  186. if (!wdata->external_size) {
  187. if (wdata->pixel_ratio != 1.0f) {
  188. /*scale canvas down*/
  189. emscripten_set_element_css_size(wdata->canvas_id, window->w, window->h);
  190. }
  191. }
  192. #if SDL_VIDEO_OPENGL_EGL
  193. if (window->flags & SDL_WINDOW_OPENGL) {
  194. if (!_this->egl_data) {
  195. if (SDL_GL_LoadLibrary(NULL) < 0) {
  196. return -1;
  197. }
  198. }
  199. wdata->egl_surface = SDL_EGL_CreateSurface(_this, 0);
  200. if (wdata->egl_surface == EGL_NO_SURFACE) {
  201. return SDL_SetError("Could not create GLES window surface");
  202. }
  203. }
  204. #endif
  205. wdata->window = window;
  206. /* Setup driver data for this window */
  207. window->driverdata = wdata;
  208. /* One window, it always has focus */
  209. SDL_SetMouseFocus(window);
  210. SDL_SetKeyboardFocus(window);
  211. Emscripten_RegisterEventHandlers(wdata);
  212. /* Window has been successfully created */
  213. return 0;
  214. }
  215. static void Emscripten_SetWindowSize(_THIS, SDL_Window * window)
  216. {
  217. SDL_WindowData *data;
  218. if (window->driverdata) {
  219. data = (SDL_WindowData *) window->driverdata;
  220. /* update pixel ratio */
  221. if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
  222. data->pixel_ratio = emscripten_get_device_pixel_ratio();
  223. }
  224. emscripten_set_canvas_element_size(data->canvas_id, window->w * data->pixel_ratio, window->h * data->pixel_ratio);
  225. /*scale canvas down*/
  226. if (!data->external_size && data->pixel_ratio != 1.0f) {
  227. emscripten_set_element_css_size(data->canvas_id, window->w, window->h);
  228. }
  229. }
  230. }
  231. static void
  232. Emscripten_DestroyWindow(_THIS, SDL_Window * window)
  233. {
  234. SDL_WindowData *data;
  235. if(window->driverdata) {
  236. data = (SDL_WindowData *) window->driverdata;
  237. Emscripten_UnregisterEventHandlers(data);
  238. #if SDL_VIDEO_OPENGL_EGL
  239. if (data->egl_surface != EGL_NO_SURFACE) {
  240. SDL_EGL_DestroySurface(_this, data->egl_surface);
  241. data->egl_surface = EGL_NO_SURFACE;
  242. }
  243. #endif
  244. /* We can't destroy the canvas, so resize it to zero instead */
  245. emscripten_set_canvas_element_size(data->canvas_id, 0, 0);
  246. SDL_free(data->canvas_id);
  247. SDL_free(window->driverdata);
  248. window->driverdata = NULL;
  249. }
  250. }
  251. static void
  252. Emscripten_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
  253. {
  254. SDL_WindowData *data;
  255. if(window->driverdata) {
  256. data = (SDL_WindowData *) window->driverdata;
  257. if(fullscreen) {
  258. EmscriptenFullscreenStrategy strategy;
  259. SDL_bool is_desktop_fullscreen = (window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP;
  260. int res;
  261. strategy.scaleMode = is_desktop_fullscreen ? EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH : EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT;
  262. if(!is_desktop_fullscreen) {
  263. strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE;
  264. } else if(window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
  265. strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_HIDEF;
  266. } else {
  267. strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
  268. }
  269. strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
  270. strategy.canvasResizedCallback = Emscripten_HandleCanvasResize;
  271. strategy.canvasResizedCallbackUserData = data;
  272. data->requested_fullscreen_mode = window->flags & (SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
  273. data->fullscreen_resize = is_desktop_fullscreen;
  274. res = emscripten_request_fullscreen_strategy(data->canvas_id, 1, &strategy);
  275. if(res != EMSCRIPTEN_RESULT_SUCCESS && res != EMSCRIPTEN_RESULT_DEFERRED) {
  276. /* unset flags, fullscreen failed */
  277. window->flags &= ~(SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_FULLSCREEN);
  278. }
  279. }
  280. else
  281. emscripten_exit_fullscreen();
  282. }
  283. }
  284. static void
  285. Emscripten_SetWindowTitle(_THIS, SDL_Window * window) {
  286. emscripten_set_window_title(window->title);
  287. }
  288. #endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
  289. /* vi: set ts=4 sw=4 expandtab: */