SDL_emscriptenvideo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #ifdef SDL_VIDEO_DRIVER_EMSCRIPTEN
  20. #include "../SDL_sysvideo.h"
  21. #include "../SDL_pixels_c.h"
  22. #include "../../events/SDL_events_c.h"
  23. #include "SDL_emscriptenvideo.h"
  24. #include "SDL_emscriptenopengles.h"
  25. #include "SDL_emscriptenframebuffer.h"
  26. #include "SDL_emscriptenevents.h"
  27. #include "SDL_emscriptenmouse.h"
  28. #define EMSCRIPTENVID_DRIVER_NAME "emscripten"
  29. // Initialization/Query functions
  30. static bool Emscripten_VideoInit(SDL_VideoDevice *_this);
  31. static bool Emscripten_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode);
  32. static void Emscripten_VideoQuit(SDL_VideoDevice *_this);
  33. static bool Emscripten_GetDisplayUsableBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect);
  34. static bool Emscripten_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props);
  35. static void Emscripten_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window);
  36. static void Emscripten_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h);
  37. static void Emscripten_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window);
  38. static SDL_FullscreenResult Emscripten_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *display, SDL_FullscreenOp fullscreen);
  39. static void Emscripten_PumpEvents(SDL_VideoDevice *_this);
  40. static void Emscripten_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window);
  41. // Emscripten driver bootstrap functions
  42. static void Emscripten_DeleteDevice(SDL_VideoDevice *device)
  43. {
  44. SDL_free(device);
  45. }
  46. static SDL_SystemTheme Emscripten_GetSystemTheme(void)
  47. {
  48. /* Technically, light theme can mean explicit light theme or no preference.
  49. https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme#syntax */
  50. int theme_code = EM_ASM_INT({
  51. if (!window.matchMedia) {
  52. return -1;
  53. }
  54. if (window.matchMedia('(prefers-color-scheme: light)').matches) {
  55. return 0;
  56. }
  57. if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
  58. return 1;
  59. }
  60. return -1;
  61. });
  62. switch (theme_code) {
  63. case 0:
  64. return SDL_SYSTEM_THEME_LIGHT;
  65. case 1:
  66. return SDL_SYSTEM_THEME_DARK;
  67. default:
  68. return SDL_SYSTEM_THEME_UNKNOWN;
  69. }
  70. }
  71. static void Emscripten_ListenSystemTheme(void)
  72. {
  73. MAIN_THREAD_EM_ASM({
  74. if (window.matchMedia) {
  75. if (typeof(Module['SDL3']) === 'undefined') {
  76. Module['SDL3'] = {};
  77. }
  78. var SDL3 = Module['SDL3'];
  79. SDL3.eventHandlerThemeChanged = function(event) {
  80. _Emscripten_SendSystemThemeChangedEvent();
  81. };
  82. SDL3.themeChangedMatchMedia = window.matchMedia('(prefers-color-scheme: dark)');
  83. SDL3.themeChangedMatchMedia.addEventListener('change', SDL3.eventHandlerThemeChanged);
  84. }
  85. });
  86. }
  87. static void Emscripten_UnlistenSystemTheme(void)
  88. {
  89. MAIN_THREAD_EM_ASM({
  90. if (typeof(Module['SDL3']) !== 'undefined') {
  91. var SDL3 = Module['SDL3'];
  92. SDL3.themeChangedMatchMedia.removeEventListener('change', SDL3.eventHandlerThemeChanged);
  93. SDL3.themeChangedMatchMedia = undefined;
  94. SDL3.eventHandlerThemeChanged = undefined;
  95. }
  96. });
  97. }
  98. EMSCRIPTEN_KEEPALIVE void Emscripten_SendSystemThemeChangedEvent(void)
  99. {
  100. SDL_SetSystemTheme(Emscripten_GetSystemTheme());
  101. }
  102. static SDL_VideoDevice *Emscripten_CreateDevice(void)
  103. {
  104. SDL_VideoDevice *device;
  105. // Initialize all variables that we clean on shutdown
  106. device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice));
  107. if (!device) {
  108. return NULL;
  109. }
  110. /* Firefox sends blur event which would otherwise prevent full screen
  111. * when the user clicks to allow full screen.
  112. * See https://bugzilla.mozilla.org/show_bug.cgi?id=1144964
  113. */
  114. SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
  115. // Set the function pointers
  116. device->VideoInit = Emscripten_VideoInit;
  117. device->VideoQuit = Emscripten_VideoQuit;
  118. device->GetDisplayUsableBounds = Emscripten_GetDisplayUsableBounds;
  119. device->SetDisplayMode = Emscripten_SetDisplayMode;
  120. device->PumpEvents = Emscripten_PumpEvents;
  121. device->CreateSDLWindow = Emscripten_CreateWindow;
  122. device->SetWindowTitle = Emscripten_SetWindowTitle;
  123. /*device->SetWindowIcon = Emscripten_SetWindowIcon;
  124. device->SetWindowPosition = Emscripten_SetWindowPosition;*/
  125. device->SetWindowSize = Emscripten_SetWindowSize;
  126. /*device->ShowWindow = Emscripten_ShowWindow;
  127. device->HideWindow = Emscripten_HideWindow;
  128. device->RaiseWindow = Emscripten_RaiseWindow;
  129. device->MaximizeWindow = Emscripten_MaximizeWindow;
  130. device->MinimizeWindow = Emscripten_MinimizeWindow;
  131. device->RestoreWindow = Emscripten_RestoreWindow;
  132. device->SetWindowMouseGrab = Emscripten_SetWindowMouseGrab;*/
  133. device->GetWindowSizeInPixels = Emscripten_GetWindowSizeInPixels;
  134. device->DestroyWindow = Emscripten_DestroyWindow;
  135. device->SetWindowFullscreen = Emscripten_SetWindowFullscreen;
  136. device->CreateWindowFramebuffer = Emscripten_CreateWindowFramebuffer;
  137. device->UpdateWindowFramebuffer = Emscripten_UpdateWindowFramebuffer;
  138. device->DestroyWindowFramebuffer = Emscripten_DestroyWindowFramebuffer;
  139. device->GL_LoadLibrary = Emscripten_GLES_LoadLibrary;
  140. device->GL_GetProcAddress = Emscripten_GLES_GetProcAddress;
  141. device->GL_UnloadLibrary = Emscripten_GLES_UnloadLibrary;
  142. device->GL_CreateContext = Emscripten_GLES_CreateContext;
  143. device->GL_MakeCurrent = Emscripten_GLES_MakeCurrent;
  144. device->GL_SetSwapInterval = Emscripten_GLES_SetSwapInterval;
  145. device->GL_GetSwapInterval = Emscripten_GLES_GetSwapInterval;
  146. device->GL_SwapWindow = Emscripten_GLES_SwapWindow;
  147. device->GL_DestroyContext = Emscripten_GLES_DestroyContext;
  148. device->free = Emscripten_DeleteDevice;
  149. Emscripten_ListenSystemTheme();
  150. device->system_theme = Emscripten_GetSystemTheme();
  151. return device;
  152. }
  153. VideoBootStrap Emscripten_bootstrap = {
  154. EMSCRIPTENVID_DRIVER_NAME, "SDL emscripten video driver",
  155. Emscripten_CreateDevice,
  156. NULL // no ShowMessageBox implementation
  157. };
  158. bool Emscripten_VideoInit(SDL_VideoDevice *_this)
  159. {
  160. SDL_DisplayMode mode;
  161. // Use a fake 32-bpp desktop mode
  162. SDL_zero(mode);
  163. mode.format = SDL_PIXELFORMAT_XRGB8888;
  164. emscripten_get_screen_size(&mode.w, &mode.h);
  165. mode.pixel_density = emscripten_get_device_pixel_ratio();
  166. if (SDL_AddBasicVideoDisplay(&mode) == 0) {
  167. return false;
  168. }
  169. Emscripten_InitMouse();
  170. // Assume we have a mouse and keyboard
  171. SDL_AddKeyboard(SDL_DEFAULT_KEYBOARD_ID, NULL, false);
  172. SDL_AddMouse(SDL_DEFAULT_MOUSE_ID, NULL, false);
  173. // We're done!
  174. return true;
  175. }
  176. static bool Emscripten_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
  177. {
  178. // can't do this
  179. return true;
  180. }
  181. static void Emscripten_VideoQuit(SDL_VideoDevice *_this)
  182. {
  183. Emscripten_QuitMouse();
  184. Emscripten_UnlistenSystemTheme();
  185. }
  186. static bool Emscripten_GetDisplayUsableBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_Rect *rect)
  187. {
  188. if (rect) {
  189. rect->x = 0;
  190. rect->y = 0;
  191. rect->w = MAIN_THREAD_EM_ASM_INT({
  192. return window.innerWidth;
  193. });
  194. rect->h = MAIN_THREAD_EM_ASM_INT({
  195. return window.innerHeight;
  196. });
  197. }
  198. return true;
  199. }
  200. static void Emscripten_PumpEvents(SDL_VideoDevice *_this)
  201. {
  202. // do nothing.
  203. }
  204. static bool Emscripten_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID props)
  205. {
  206. SDL_WindowData *wdata;
  207. double scaled_w, scaled_h;
  208. double css_w, css_h;
  209. const char *selector;
  210. // Allocate window internal data
  211. wdata = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData));
  212. if (!wdata) {
  213. return false;
  214. }
  215. selector = SDL_GetHint(SDL_HINT_EMSCRIPTEN_CANVAS_SELECTOR);
  216. if (!selector) {
  217. selector = "#canvas";
  218. }
  219. wdata->canvas_id = SDL_strdup(selector);
  220. if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
  221. wdata->pixel_ratio = emscripten_get_device_pixel_ratio();
  222. } else {
  223. wdata->pixel_ratio = 1.0f;
  224. }
  225. scaled_w = SDL_floor(window->w * wdata->pixel_ratio);
  226. scaled_h = SDL_floor(window->h * wdata->pixel_ratio);
  227. // set a fake size to check if there is any CSS sizing the canvas
  228. emscripten_set_canvas_element_size(wdata->canvas_id, 1, 1);
  229. emscripten_get_element_css_size(wdata->canvas_id, &css_w, &css_h);
  230. wdata->external_size = SDL_floor(css_w) != 1 || SDL_floor(css_h) != 1;
  231. if ((window->flags & SDL_WINDOW_RESIZABLE) && wdata->external_size) {
  232. // external css has resized us
  233. scaled_w = css_w * wdata->pixel_ratio;
  234. scaled_h = css_h * wdata->pixel_ratio;
  235. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, SDL_lroundf(css_w), SDL_lroundf(css_h));
  236. }
  237. emscripten_set_canvas_element_size(wdata->canvas_id, SDL_lroundf(scaled_w), SDL_lroundf(scaled_h));
  238. // if the size is not being controlled by css, we need to scale down for hidpi
  239. if (!wdata->external_size) {
  240. if (wdata->pixel_ratio != 1.0f) {
  241. // scale canvas down
  242. emscripten_set_element_css_size(wdata->canvas_id, window->w, window->h);
  243. }
  244. }
  245. wdata->window = window;
  246. // Setup driver data for this window
  247. window->internal = wdata;
  248. // One window, it always has focus
  249. SDL_SetMouseFocus(window);
  250. SDL_SetKeyboardFocus(window);
  251. Emscripten_RegisterEventHandlers(wdata);
  252. // Window has been successfully created
  253. return true;
  254. }
  255. static void Emscripten_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window)
  256. {
  257. SDL_WindowData *data;
  258. if (window->internal) {
  259. data = window->internal;
  260. // update pixel ratio
  261. if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
  262. data->pixel_ratio = emscripten_get_device_pixel_ratio();
  263. }
  264. emscripten_set_canvas_element_size(data->canvas_id, SDL_lroundf(window->floating.w * data->pixel_ratio), SDL_lroundf(window->floating.h * data->pixel_ratio));
  265. // scale canvas down
  266. if (!data->external_size && data->pixel_ratio != 1.0f) {
  267. emscripten_set_element_css_size(data->canvas_id, window->floating.w, window->floating.h);
  268. }
  269. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->floating.w, window->floating.h);
  270. }
  271. }
  272. static void Emscripten_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h)
  273. {
  274. SDL_WindowData *data;
  275. if (window->internal) {
  276. data = window->internal;
  277. *w = SDL_lroundf(window->w * data->pixel_ratio);
  278. *h = SDL_lroundf(window->h * data->pixel_ratio);
  279. }
  280. }
  281. static void Emscripten_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
  282. {
  283. SDL_WindowData *data;
  284. if (window->internal) {
  285. data = window->internal;
  286. Emscripten_UnregisterEventHandlers(data);
  287. // We can't destroy the canvas, so resize it to zero instead
  288. emscripten_set_canvas_element_size(data->canvas_id, 0, 0);
  289. SDL_free(data->canvas_id);
  290. SDL_free(window->internal);
  291. window->internal = NULL;
  292. }
  293. }
  294. static SDL_FullscreenResult Emscripten_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window *window, SDL_VideoDisplay *display, SDL_FullscreenOp fullscreen)
  295. {
  296. SDL_WindowData *data;
  297. int res = -1;
  298. if (window->internal) {
  299. data = window->internal;
  300. if (fullscreen) {
  301. EmscriptenFullscreenStrategy strategy;
  302. bool is_fullscreen_desktop = !window->fullscreen_exclusive;
  303. SDL_zero(strategy);
  304. strategy.scaleMode = is_fullscreen_desktop ? EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH : EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT;
  305. if (!is_fullscreen_desktop) {
  306. strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE;
  307. } else if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
  308. strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_HIDEF;
  309. } else {
  310. strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
  311. }
  312. strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
  313. strategy.canvasResizedCallback = Emscripten_HandleCanvasResize;
  314. strategy.canvasResizedCallbackUserData = data;
  315. data->fullscreen_mode_flags = (window->flags & SDL_WINDOW_FULLSCREEN);
  316. data->fullscreen_resize = is_fullscreen_desktop;
  317. res = emscripten_request_fullscreen_strategy(data->canvas_id, 1, &strategy);
  318. } else {
  319. res = emscripten_exit_fullscreen();
  320. }
  321. }
  322. if (res == EMSCRIPTEN_RESULT_SUCCESS) {
  323. return SDL_FULLSCREEN_SUCCEEDED;
  324. } else if (res == EMSCRIPTEN_RESULT_DEFERRED) {
  325. return SDL_FULLSCREEN_PENDING;
  326. } else {
  327. return SDL_FULLSCREEN_FAILED;
  328. }
  329. }
  330. static void Emscripten_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window)
  331. {
  332. emscripten_set_window_title(window->title);
  333. }
  334. #endif // SDL_VIDEO_DRIVER_EMSCRIPTEN