display_server_javascript.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. #include "platform/javascript/display_server_javascript.h"
  2. #include "drivers/dummy/rasterizer_dummy.h"
  3. #include "platform/javascript/os_javascript.h"
  4. #include <emscripten.h>
  5. #include <png.h>
  6. #include "dom_keys.inc"
  7. #define DOM_BUTTON_LEFT 0
  8. #define DOM_BUTTON_MIDDLE 1
  9. #define DOM_BUTTON_RIGHT 2
  10. #define DOM_BUTTON_XBUTTON1 3
  11. #define DOM_BUTTON_XBUTTON2 4
  12. DisplayServerJavaScript *DisplayServerJavaScript::get_singleton() {
  13. return static_cast<DisplayServerJavaScript *>(DisplayServer::get_singleton());
  14. }
  15. // Window (canvas)
  16. extern "C" EMSCRIPTEN_KEEPALIVE void _set_canvas_id(uint8_t *p_data, int p_data_size) {
  17. DisplayServerJavaScript *display = DisplayServerJavaScript::get_singleton();
  18. display->canvas_id.parse_utf8((const char *)p_data, p_data_size);
  19. display->canvas_id = "#" + display->canvas_id;
  20. }
  21. static void focus_canvas() {
  22. /* clang-format off */
  23. EM_ASM(
  24. Module['canvas'].focus();
  25. );
  26. /* clang-format on */
  27. }
  28. static bool is_canvas_focused() {
  29. /* clang-format off */
  30. return EM_ASM_INT_V(
  31. return document.activeElement == Module['canvas'];
  32. );
  33. /* clang-format on */
  34. }
  35. static Point2 compute_position_in_canvas(int x, int y) {
  36. DisplayServerJavaScript *display = DisplayServerJavaScript::get_singleton();
  37. int canvas_x = EM_ASM_INT({
  38. return Module['canvas'].getBoundingClientRect().x;
  39. });
  40. int canvas_y = EM_ASM_INT({
  41. return Module['canvas'].getBoundingClientRect().y;
  42. });
  43. int canvas_width;
  44. int canvas_height;
  45. emscripten_get_canvas_element_size(display->canvas_id.utf8().get_data(), &canvas_width, &canvas_height);
  46. double element_width;
  47. double element_height;
  48. emscripten_get_element_css_size(display->canvas_id.utf8().get_data(), &element_width, &element_height);
  49. return Point2((int)(canvas_width / element_width * (x - canvas_x)),
  50. (int)(canvas_height / element_height * (y - canvas_y)));
  51. }
  52. static bool cursor_inside_canvas = true;
  53. EM_BOOL DisplayServerJavaScript::fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data) {
  54. DisplayServerJavaScript *display = get_singleton();
  55. // Empty ID is canvas.
  56. String target_id = String::utf8(p_event->id);
  57. if (target_id.empty() || "#" + target_id == display->canvas_id) {
  58. // This event property is the only reliable data on
  59. // browser fullscreen state.
  60. if (p_event->isFullscreen) {
  61. display->window_mode = WINDOW_MODE_FULLSCREEN;
  62. } else {
  63. display->window_mode = WINDOW_MODE_WINDOWED;
  64. }
  65. }
  66. return false;
  67. }
  68. // Keys
  69. template <typename T>
  70. static void dom2godot_mod(T *emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event) {
  71. godot_event->set_shift(emscripten_event_ptr->shiftKey);
  72. godot_event->set_alt(emscripten_event_ptr->altKey);
  73. godot_event->set_control(emscripten_event_ptr->ctrlKey);
  74. godot_event->set_metakey(emscripten_event_ptr->metaKey);
  75. }
  76. static Ref<InputEventKey> setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
  77. Ref<InputEventKey> ev;
  78. ev.instance();
  79. ev->set_echo(emscripten_event->repeat);
  80. dom2godot_mod(emscripten_event, ev);
  81. ev->set_keycode(dom2godot_keycode(emscripten_event->keyCode));
  82. ev->set_physical_keycode(dom2godot_keycode(emscripten_event->keyCode));
  83. String unicode = String::utf8(emscripten_event->key);
  84. // Check if empty or multi-character (e.g. `CapsLock`).
  85. if (unicode.length() != 1) {
  86. // Might be empty as well, but better than nonsense.
  87. unicode = String::utf8(emscripten_event->charValue);
  88. }
  89. if (unicode.length() == 1) {
  90. ev->set_unicode(unicode[0]);
  91. }
  92. return ev;
  93. }
  94. EM_BOOL DisplayServerJavaScript::keydown_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data) {
  95. DisplayServerJavaScript *display = get_singleton();
  96. Ref<InputEventKey> ev = setup_key_event(p_event);
  97. ev->set_pressed(true);
  98. if (ev->get_unicode() == 0 && keycode_has_unicode(ev->get_keycode())) {
  99. // Defer to keypress event for legacy unicode retrieval.
  100. display->deferred_key_event = ev;
  101. // Do not suppress keypress event.
  102. return false;
  103. }
  104. Input::get_singleton()->parse_input_event(ev);
  105. return true;
  106. }
  107. EM_BOOL DisplayServerJavaScript::keypress_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data) {
  108. DisplayServerJavaScript *display = get_singleton();
  109. display->deferred_key_event->set_unicode(p_event->charCode);
  110. Input::get_singleton()->parse_input_event(display->deferred_key_event);
  111. return true;
  112. }
  113. EM_BOOL DisplayServerJavaScript::keyup_callback(int p_event_type, const EmscriptenKeyboardEvent *p_event, void *p_user_data) {
  114. Ref<InputEventKey> ev = setup_key_event(p_event);
  115. ev->set_pressed(false);
  116. Input::get_singleton()->parse_input_event(ev);
  117. return ev->get_keycode() != KEY_UNKNOWN && ev->get_keycode() != 0;
  118. }
  119. // Mouse
  120. EM_BOOL DisplayServerJavaScript::mouse_button_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data) {
  121. DisplayServerJavaScript *display = get_singleton();
  122. Ref<InputEventMouseButton> ev;
  123. ev.instance();
  124. ev->set_pressed(p_event_type == EMSCRIPTEN_EVENT_MOUSEDOWN);
  125. ev->set_position(compute_position_in_canvas(p_event->clientX, p_event->clientY));
  126. ev->set_global_position(ev->get_position());
  127. dom2godot_mod(p_event, ev);
  128. switch (p_event->button) {
  129. case DOM_BUTTON_LEFT:
  130. ev->set_button_index(BUTTON_LEFT);
  131. break;
  132. case DOM_BUTTON_MIDDLE:
  133. ev->set_button_index(BUTTON_MIDDLE);
  134. break;
  135. case DOM_BUTTON_RIGHT:
  136. ev->set_button_index(BUTTON_RIGHT);
  137. break;
  138. case DOM_BUTTON_XBUTTON1:
  139. ev->set_button_index(BUTTON_XBUTTON1);
  140. break;
  141. case DOM_BUTTON_XBUTTON2:
  142. ev->set_button_index(BUTTON_XBUTTON2);
  143. break;
  144. default:
  145. return false;
  146. }
  147. if (ev->is_pressed()) {
  148. double diff = emscripten_get_now() - display->last_click_ms;
  149. if (ev->get_button_index() == display->last_click_button_index) {
  150. if (diff < 400 && Point2(display->last_click_pos).distance_to(ev->get_position()) < 5) {
  151. display->last_click_ms = 0;
  152. display->last_click_pos = Point2(-100, -100);
  153. display->last_click_button_index = -1;
  154. ev->set_doubleclick(true);
  155. }
  156. } else {
  157. display->last_click_button_index = ev->get_button_index();
  158. }
  159. if (!ev->is_doubleclick()) {
  160. display->last_click_ms += diff;
  161. display->last_click_pos = ev->get_position();
  162. }
  163. }
  164. Input *input = Input::get_singleton();
  165. int mask = input->get_mouse_button_mask();
  166. int button_flag = 1 << (ev->get_button_index() - 1);
  167. if (ev->is_pressed()) {
  168. // Since the event is consumed, focus manually. The containing iframe,
  169. // if exists, may not have focus yet, so focus even if already focused.
  170. focus_canvas();
  171. mask |= button_flag;
  172. } else if (mask & button_flag) {
  173. mask &= ~button_flag;
  174. } else {
  175. // Received release event, but press was outside the canvas, so ignore.
  176. return false;
  177. }
  178. ev->set_button_mask(mask);
  179. input->parse_input_event(ev);
  180. // Prevent multi-click text selection and wheel-click scrolling anchor.
  181. // Context menu is prevented through contextmenu event.
  182. return true;
  183. }
  184. EM_BOOL DisplayServerJavaScript::mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data) {
  185. Input *input = Input::get_singleton();
  186. int input_mask = input->get_mouse_button_mask();
  187. Point2 pos = compute_position_in_canvas(p_event->clientX, p_event->clientY);
  188. // For motion outside the canvas, only read mouse movement if dragging
  189. // started inside the canvas; imitating desktop app behaviour.
  190. if (!cursor_inside_canvas && !input_mask)
  191. return false;
  192. Ref<InputEventMouseMotion> ev;
  193. ev.instance();
  194. dom2godot_mod(p_event, ev);
  195. ev->set_button_mask(input_mask);
  196. ev->set_position(pos);
  197. ev->set_global_position(ev->get_position());
  198. ev->set_relative(Vector2(p_event->movementX, p_event->movementY));
  199. input->set_mouse_position(ev->get_position());
  200. ev->set_speed(input->get_last_mouse_speed());
  201. input->parse_input_event(ev);
  202. // Don't suppress mouseover/-leave events.
  203. return false;
  204. }
  205. // Cursor
  206. static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape) {
  207. switch (p_shape) {
  208. case DisplayServer::CURSOR_ARROW:
  209. return "auto";
  210. case DisplayServer::CURSOR_IBEAM:
  211. return "text";
  212. case DisplayServer::CURSOR_POINTING_HAND:
  213. return "pointer";
  214. case DisplayServer::CURSOR_CROSS:
  215. return "crosshair";
  216. case DisplayServer::CURSOR_WAIT:
  217. return "progress";
  218. case DisplayServer::CURSOR_BUSY:
  219. return "wait";
  220. case DisplayServer::CURSOR_DRAG:
  221. return "grab";
  222. case DisplayServer::CURSOR_CAN_DROP:
  223. return "grabbing";
  224. case DisplayServer::CURSOR_FORBIDDEN:
  225. return "no-drop";
  226. case DisplayServer::CURSOR_VSIZE:
  227. return "ns-resize";
  228. case DisplayServer::CURSOR_HSIZE:
  229. return "ew-resize";
  230. case DisplayServer::CURSOR_BDIAGSIZE:
  231. return "nesw-resize";
  232. case DisplayServer::CURSOR_FDIAGSIZE:
  233. return "nwse-resize";
  234. case DisplayServer::CURSOR_MOVE:
  235. return "move";
  236. case DisplayServer::CURSOR_VSPLIT:
  237. return "row-resize";
  238. case DisplayServer::CURSOR_HSPLIT:
  239. return "col-resize";
  240. case DisplayServer::CURSOR_HELP:
  241. return "help";
  242. default:
  243. return "auto";
  244. }
  245. }
  246. static void set_css_cursor(const char *p_cursor) {
  247. /* clang-format off */
  248. EM_ASM_({
  249. Module['canvas'].style.cursor = UTF8ToString($0);
  250. }, p_cursor);
  251. /* clang-format on */
  252. }
  253. static bool is_css_cursor_hidden() {
  254. /* clang-format off */
  255. return EM_ASM_INT({
  256. return Module['canvas'].style.cursor === 'none';
  257. });
  258. /* clang-format on */
  259. }
  260. void DisplayServerJavaScript::cursor_set_shape(CursorShape p_shape) {
  261. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  262. if (mouse_get_mode() == MOUSE_MODE_VISIBLE) {
  263. if (cursors[p_shape] != "") {
  264. Vector<String> url = cursors[p_shape].split("?");
  265. set_css_cursor(("url(\"" + url[0] + "\") " + url[1] + ", auto").utf8());
  266. } else {
  267. set_css_cursor(godot2dom_cursor(p_shape));
  268. }
  269. }
  270. cursor_shape = p_shape;
  271. }
  272. DisplayServer::CursorShape DisplayServerJavaScript::cursor_get_shape() const {
  273. return cursor_shape;
  274. }
  275. void DisplayServerJavaScript::cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  276. if (p_cursor.is_valid()) {
  277. Map<CursorShape, Vector<Variant>>::Element *cursor_c = cursors_cache.find(p_shape);
  278. if (cursor_c) {
  279. if (cursor_c->get()[0] == p_cursor && cursor_c->get()[1] == p_hotspot) {
  280. cursor_set_shape(p_shape);
  281. return;
  282. }
  283. cursors_cache.erase(p_shape);
  284. }
  285. Ref<Texture2D> texture = p_cursor;
  286. Ref<AtlasTexture> atlas_texture = p_cursor;
  287. Ref<Image> image;
  288. Size2 texture_size;
  289. Rect2 atlas_rect;
  290. if (texture.is_valid()) {
  291. image = texture->get_data();
  292. }
  293. if (!image.is_valid() && atlas_texture.is_valid()) {
  294. texture = atlas_texture->get_atlas();
  295. atlas_rect.size.width = texture->get_width();
  296. atlas_rect.size.height = texture->get_height();
  297. atlas_rect.position.x = atlas_texture->get_region().position.x;
  298. atlas_rect.position.y = atlas_texture->get_region().position.y;
  299. texture_size.width = atlas_texture->get_region().size.x;
  300. texture_size.height = atlas_texture->get_region().size.y;
  301. } else if (image.is_valid()) {
  302. texture_size.width = texture->get_width();
  303. texture_size.height = texture->get_height();
  304. }
  305. ERR_FAIL_COND(!texture.is_valid());
  306. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  307. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  308. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  309. image = texture->get_data();
  310. ERR_FAIL_COND(!image.is_valid());
  311. image = image->duplicate();
  312. if (atlas_texture.is_valid())
  313. image->crop_from_point(
  314. atlas_rect.position.x,
  315. atlas_rect.position.y,
  316. texture_size.width,
  317. texture_size.height);
  318. if (image->get_format() != Image::FORMAT_RGBA8) {
  319. image->convert(Image::FORMAT_RGBA8);
  320. }
  321. png_image png_meta;
  322. memset(&png_meta, 0, sizeof png_meta);
  323. png_meta.version = PNG_IMAGE_VERSION;
  324. png_meta.width = texture_size.width;
  325. png_meta.height = texture_size.height;
  326. png_meta.format = PNG_FORMAT_RGBA;
  327. PackedByteArray png;
  328. size_t len;
  329. PackedByteArray data = image->get_data();
  330. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  331. png.resize(len);
  332. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  333. char *object_url;
  334. /* clang-format off */
  335. EM_ASM({
  336. var PNG_PTR = $0;
  337. var PNG_LEN = $1;
  338. var PTR = $2;
  339. var png = new Blob([HEAPU8.slice(PNG_PTR, PNG_PTR + PNG_LEN)], { type: 'image/png' });
  340. var url = URL.createObjectURL(png);
  341. var length_bytes = lengthBytesUTF8(url) + 1;
  342. var string_on_wasm_heap = _malloc(length_bytes);
  343. setValue(PTR, string_on_wasm_heap, '*');
  344. stringToUTF8(url, string_on_wasm_heap, length_bytes);
  345. }, png.ptr(), len, &object_url);
  346. /* clang-format on */
  347. String url = String::utf8(object_url) + "?" + itos(p_hotspot.x) + " " + itos(p_hotspot.y);
  348. /* clang-format off */
  349. EM_ASM({ _free($0); }, object_url);
  350. /* clang-format on */
  351. if (cursors[p_shape] != "") {
  352. /* clang-format off */
  353. EM_ASM({
  354. URL.revokeObjectURL(UTF8ToString($0).split('?')[0]);
  355. }, cursors[p_shape].utf8().get_data());
  356. /* clang-format on */
  357. cursors[p_shape] = "";
  358. }
  359. cursors[p_shape] = url;
  360. Vector<Variant> params;
  361. params.push_back(p_cursor);
  362. params.push_back(p_hotspot);
  363. cursors_cache.insert(p_shape, params);
  364. } else if (cursors[p_shape] != "") {
  365. /* clang-format off */
  366. EM_ASM({
  367. URL.revokeObjectURL(UTF8ToString($0).split('?')[0]);
  368. }, cursors[p_shape].utf8().get_data());
  369. /* clang-format on */
  370. cursors[p_shape] = "";
  371. cursors_cache.erase(p_shape);
  372. }
  373. cursor_set_shape(cursor_shape);
  374. }
  375. // Mouse mode
  376. void DisplayServerJavaScript::mouse_set_mode(MouseMode p_mode) {
  377. ERR_FAIL_COND_MSG(p_mode == MOUSE_MODE_CONFINED, "MOUSE_MODE_CONFINED is not supported for the HTML5 platform.");
  378. if (p_mode == mouse_get_mode())
  379. return;
  380. if (p_mode == MOUSE_MODE_VISIBLE) {
  381. // set_css_cursor must be called before set_cursor_shape to make the cursor visible
  382. set_css_cursor(godot2dom_cursor(cursor_shape));
  383. cursor_set_shape(cursor_shape);
  384. emscripten_exit_pointerlock();
  385. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  386. set_css_cursor("none");
  387. emscripten_exit_pointerlock();
  388. } else if (p_mode == MOUSE_MODE_CAPTURED) {
  389. EMSCRIPTEN_RESULT result = emscripten_request_pointerlock("canvas", false);
  390. ERR_FAIL_COND_MSG(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback.");
  391. ERR_FAIL_COND_MSG(result != EMSCRIPTEN_RESULT_SUCCESS, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback.");
  392. // set_css_cursor must be called before cursor_set_shape to make the cursor visible
  393. set_css_cursor(godot2dom_cursor(cursor_shape));
  394. cursor_set_shape(cursor_shape);
  395. }
  396. }
  397. DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const {
  398. if (is_css_cursor_hidden())
  399. return MOUSE_MODE_HIDDEN;
  400. EmscriptenPointerlockChangeEvent ev;
  401. emscripten_get_pointerlock_status(&ev);
  402. return (ev.isActive && String::utf8(ev.id) == "canvas") ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE;
  403. }
  404. // Wheel
  405. EM_BOOL DisplayServerJavaScript::wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data) {
  406. ERR_FAIL_COND_V(p_event_type != EMSCRIPTEN_EVENT_WHEEL, false);
  407. if (!is_canvas_focused()) {
  408. if (cursor_inside_canvas) {
  409. focus_canvas();
  410. } else {
  411. return false;
  412. }
  413. }
  414. Input *input = Input::get_singleton();
  415. Ref<InputEventMouseButton> ev;
  416. ev.instance();
  417. ev->set_position(input->get_mouse_position());
  418. ev->set_global_position(ev->get_position());
  419. ev->set_shift(input->is_key_pressed(KEY_SHIFT));
  420. ev->set_alt(input->is_key_pressed(KEY_ALT));
  421. ev->set_control(input->is_key_pressed(KEY_CONTROL));
  422. ev->set_metakey(input->is_key_pressed(KEY_META));
  423. if (p_event->deltaY < 0)
  424. ev->set_button_index(BUTTON_WHEEL_UP);
  425. else if (p_event->deltaY > 0)
  426. ev->set_button_index(BUTTON_WHEEL_DOWN);
  427. else if (p_event->deltaX > 0)
  428. ev->set_button_index(BUTTON_WHEEL_LEFT);
  429. else if (p_event->deltaX < 0)
  430. ev->set_button_index(BUTTON_WHEEL_RIGHT);
  431. else
  432. return false;
  433. // Different browsers give wildly different delta values, and we can't
  434. // interpret deltaMode, so use default value for wheel events' factor.
  435. int button_flag = 1 << (ev->get_button_index() - 1);
  436. ev->set_pressed(true);
  437. ev->set_button_mask(input->get_mouse_button_mask() | button_flag);
  438. input->parse_input_event(ev);
  439. ev->set_pressed(false);
  440. ev->set_button_mask(input->get_mouse_button_mask() & ~button_flag);
  441. input->parse_input_event(ev);
  442. return true;
  443. }
  444. // Touch
  445. EM_BOOL DisplayServerJavaScript::touch_press_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data) {
  446. DisplayServerJavaScript *display = get_singleton();
  447. Ref<InputEventScreenTouch> ev;
  448. ev.instance();
  449. int lowest_id_index = -1;
  450. for (int i = 0; i < p_event->numTouches; ++i) {
  451. const EmscriptenTouchPoint &touch = p_event->touches[i];
  452. if (lowest_id_index == -1 || touch.identifier < p_event->touches[lowest_id_index].identifier)
  453. lowest_id_index = i;
  454. if (!touch.isChanged)
  455. continue;
  456. ev->set_index(touch.identifier);
  457. ev->set_position(compute_position_in_canvas(touch.clientX, touch.clientY));
  458. display->touches[i] = ev->get_position();
  459. ev->set_pressed(p_event_type == EMSCRIPTEN_EVENT_TOUCHSTART);
  460. Input::get_singleton()->parse_input_event(ev);
  461. }
  462. // Resume audio context after input in case autoplay was denied.
  463. return true;
  464. }
  465. EM_BOOL DisplayServerJavaScript::touchmove_callback(int p_event_type, const EmscriptenTouchEvent *p_event, void *p_user_data) {
  466. DisplayServerJavaScript *display = get_singleton();
  467. Ref<InputEventScreenDrag> ev;
  468. ev.instance();
  469. int lowest_id_index = -1;
  470. for (int i = 0; i < p_event->numTouches; ++i) {
  471. const EmscriptenTouchPoint &touch = p_event->touches[i];
  472. if (lowest_id_index == -1 || touch.identifier < p_event->touches[lowest_id_index].identifier)
  473. lowest_id_index = i;
  474. if (!touch.isChanged)
  475. continue;
  476. ev->set_index(touch.identifier);
  477. ev->set_position(compute_position_in_canvas(touch.clientX, touch.clientY));
  478. Point2 &prev = display->touches[i];
  479. ev->set_relative(ev->get_position() - prev);
  480. prev = ev->get_position();
  481. Input::get_singleton()->parse_input_event(ev);
  482. }
  483. return true;
  484. }
  485. bool DisplayServerJavaScript::screen_is_touchscreen(int p_screen) const {
  486. return EM_ASM_INT({ return 'ontouchstart' in window; });
  487. }
  488. // Gamepad
  489. EM_BOOL DisplayServerJavaScript::gamepad_change_callback(int p_event_type, const EmscriptenGamepadEvent *p_event, void *p_user_data) {
  490. Input *input = Input::get_singleton();
  491. if (p_event_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) {
  492. String guid = "";
  493. if (String::utf8(p_event->mapping) == "standard")
  494. guid = "Default HTML5 Gamepad";
  495. input->joy_connection_changed(p_event->index, true, String::utf8(p_event->id), guid);
  496. } else {
  497. input->joy_connection_changed(p_event->index, false, "");
  498. }
  499. return true;
  500. }
  501. void DisplayServerJavaScript::process_joypads() {
  502. int joypad_count = emscripten_get_num_gamepads();
  503. Input *input = Input::get_singleton();
  504. for (int joypad = 0; joypad < joypad_count; joypad++) {
  505. EmscriptenGamepadEvent state;
  506. EMSCRIPTEN_RESULT query_result = emscripten_get_gamepad_status(joypad, &state);
  507. // Chromium reserves gamepads slots, so NO_DATA is an expected result.
  508. ERR_CONTINUE(query_result != EMSCRIPTEN_RESULT_SUCCESS &&
  509. query_result != EMSCRIPTEN_RESULT_NO_DATA);
  510. if (query_result == EMSCRIPTEN_RESULT_SUCCESS && state.connected) {
  511. int button_count = MIN(state.numButtons, 18);
  512. int axis_count = MIN(state.numAxes, 8);
  513. for (int button = 0; button < button_count; button++) {
  514. float value = state.analogButton[button];
  515. if (String::utf8(state.mapping) == "standard" && (button == JOY_ANALOG_L2 || button == JOY_ANALOG_R2)) {
  516. Input::JoyAxis joy_axis;
  517. joy_axis.min = 0;
  518. joy_axis.value = value;
  519. input->joy_axis(joypad, button, joy_axis);
  520. } else {
  521. input->joy_button(joypad, button, value);
  522. }
  523. }
  524. for (int axis = 0; axis < axis_count; axis++) {
  525. Input::JoyAxis joy_axis;
  526. joy_axis.min = -1;
  527. joy_axis.value = state.axis[axis];
  528. input->joy_axis(joypad, axis, joy_axis);
  529. }
  530. }
  531. }
  532. }
  533. #if 0
  534. bool DisplayServerJavaScript::is_joy_known(int p_device) {
  535. return Input::get_singleton()->is_joy_mapped(p_device);
  536. }
  537. String DisplayServerJavaScript::get_joy_guid(int p_device) const {
  538. return Input::get_singleton()->get_joy_guid_remapped(p_device);
  539. }
  540. #endif
  541. Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() {
  542. Vector<String> drivers;
  543. drivers.push_back("dummy");
  544. return drivers;
  545. }
  546. // Clipboard
  547. extern "C" EMSCRIPTEN_KEEPALIVE void update_clipboard(const char *p_text) {
  548. // Only call set_clipboard from OS (sets local clipboard)
  549. DisplayServerJavaScript::get_singleton()->clipboard = p_text;
  550. }
  551. void DisplayServerJavaScript::clipboard_set(const String &p_text) {
  552. /* clang-format off */
  553. int err = EM_ASM_INT({
  554. var text = UTF8ToString($0);
  555. if (!navigator.clipboard || !navigator.clipboard.writeText)
  556. return 1;
  557. navigator.clipboard.writeText(text).catch(function(e) {
  558. // Setting OS clipboard is only possible from an input callback.
  559. console.error("Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:", e);
  560. });
  561. return 0;
  562. }, p_text.utf8().get_data());
  563. /* clang-format on */
  564. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  565. }
  566. String DisplayServerJavaScript::clipboard_get() const {
  567. /* clang-format off */
  568. EM_ASM({
  569. try {
  570. navigator.clipboard.readText().then(function (result) {
  571. ccall('update_clipboard', 'void', ['string'], [result]);
  572. }).catch(function (e) {
  573. // Fail graciously.
  574. });
  575. } catch (e) {
  576. // Fail graciously.
  577. }
  578. });
  579. /* clang-format on */
  580. return clipboard;
  581. }
  582. extern "C" EMSCRIPTEN_KEEPALIVE void send_window_event(int p_notification) {
  583. if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
  584. cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  585. }
  586. OS_JavaScript *os = OS_JavaScript::get_singleton();
  587. if (os->is_finalizing())
  588. return; // We don't want events anymore.
  589. DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton();
  590. if (ds && !ds->window_event_callback.is_null()) {
  591. Variant event = int(p_notification);
  592. Variant *eventp = &event;
  593. Variant ret;
  594. Callable::CallError ce;
  595. ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce);
  596. }
  597. }
  598. void DisplayServerJavaScript::alert(const String &p_alert, const String &p_title) {
  599. /* clang-format off */
  600. EM_ASM_({
  601. window.alert(UTF8ToString($0));
  602. }, p_alert.utf8().get_data());
  603. /* clang-format on */
  604. }
  605. void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) {
  606. ERR_FAIL_COND(p_icon.is_null());
  607. Ref<Image> icon = p_icon;
  608. if (icon->is_compressed()) {
  609. icon = icon->duplicate();
  610. ERR_FAIL_COND(icon->decompress() != OK);
  611. }
  612. if (icon->get_format() != Image::FORMAT_RGBA8) {
  613. if (icon == p_icon)
  614. icon = icon->duplicate();
  615. icon->convert(Image::FORMAT_RGBA8);
  616. }
  617. png_image png_meta;
  618. memset(&png_meta, 0, sizeof png_meta);
  619. png_meta.version = PNG_IMAGE_VERSION;
  620. png_meta.width = icon->get_width();
  621. png_meta.height = icon->get_height();
  622. png_meta.format = PNG_FORMAT_RGBA;
  623. PackedByteArray png;
  624. size_t len;
  625. PackedByteArray data = icon->get_data();
  626. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  627. png.resize(len);
  628. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  629. /* clang-format off */
  630. EM_ASM({
  631. var PNG_PTR = $0;
  632. var PNG_LEN = $1;
  633. var png = new Blob([HEAPU8.slice(PNG_PTR, PNG_PTR + PNG_LEN)], { type: "image/png" });
  634. var url = URL.createObjectURL(png);
  635. var link = document.getElementById('-gd-engine-icon');
  636. if (link === null) {
  637. link = document.createElement('link');
  638. link.rel = 'icon';
  639. link.id = '-gd-engine-icon';
  640. document.head.appendChild(link);
  641. }
  642. link.href = url;
  643. }, png.ptr(), len);
  644. /* clang-format on */
  645. }
  646. void DisplayServerJavaScript::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  647. OS_JavaScript *os = OS_JavaScript::get_singleton();
  648. if (os->is_finalizing())
  649. return; // We don't want events anymore.
  650. // Resume audio context after input in case autoplay was denied.
  651. os->resume_audio();
  652. Callable cb = get_singleton()->input_event_callback;
  653. if (!cb.is_null()) {
  654. Variant ev = p_event;
  655. Variant *evp = &ev;
  656. Variant ret;
  657. Callable::CallError ce;
  658. cb.call((const Variant **)&evp, 1, ret, ce);
  659. }
  660. }
  661. DisplayServer *DisplayServerJavaScript::create_func(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  662. return memnew(DisplayServerJavaScript(p_rendering_driver, p_mode, p_flags, p_resolution, r_error));
  663. }
  664. DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) {
  665. /* clang-format off */
  666. EM_ASM({
  667. const canvas = Module['canvas'];
  668. var enc = new TextEncoder("utf-8");
  669. var buffer = new Uint8Array(enc.encode(canvas.id));
  670. var len = buffer.byteLength;
  671. var out = _malloc(len);
  672. HEAPU8.set(buffer, out);
  673. ccall("_set_canvas_id",
  674. "void",
  675. ["number", "number"],
  676. [out, len]
  677. );
  678. _free(out);
  679. });
  680. /* clang-format on */
  681. RasterizerDummy::make_current(); // TODO GLES2 in Godot 4.0... or webgpu?
  682. #if 0
  683. EmscriptenWebGLContextAttributes attributes;
  684. emscripten_webgl_init_context_attributes(&attributes);
  685. attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
  686. attributes.antialias = false;
  687. ERR_FAIL_INDEX_V(p_video_driver, VIDEO_DRIVER_MAX, ERR_INVALID_PARAMETER);
  688. if (p_desired.layered) {
  689. set_window_per_pixel_transparency_enabled(true);
  690. }
  691. bool gl_initialization_error = false;
  692. if (RasterizerGLES2::is_viable() == OK) {
  693. attributes.majorVersion = 1;
  694. RasterizerGLES2::register_config();
  695. RasterizerGLES2::make_current();
  696. } else {
  697. gl_initialization_error = true;
  698. }
  699. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(canvas_id.utf8().get_data(), &attributes);
  700. if (emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
  701. gl_initialization_error = true;
  702. }
  703. if (gl_initialization_error) {
  704. OS::get_singleton()->alert("Your browser does not seem to support WebGL. Please update your browser version.",
  705. "Unable to initialize video driver");
  706. return ERR_UNAVAILABLE;
  707. }
  708. video_driver_index = p_video_driver;
  709. #endif
  710. /* clang-format off */
  711. window_set_mode(p_mode);
  712. if (EM_ASM_INT_V({ return Module['resizeCanvasOnStart'] })) {
  713. /* clang-format on */
  714. window_set_size(p_resolution);
  715. }
  716. EMSCRIPTEN_RESULT result;
  717. CharString id = canvas_id.utf8();
  718. #define EM_CHECK(ev) \
  719. if (result != EMSCRIPTEN_RESULT_SUCCESS) \
  720. ERR_PRINT("Error while setting " #ev " callback: Code " + itos(result));
  721. #define SET_EM_CALLBACK(target, ev, cb) \
  722. result = emscripten_set_##ev##_callback(target, nullptr, true, &cb); \
  723. EM_CHECK(ev)
  724. #define SET_EM_CALLBACK_NOTARGET(ev, cb) \
  725. result = emscripten_set_##ev##_callback(nullptr, true, &cb); \
  726. EM_CHECK(ev)
  727. // These callbacks from Emscripten's html5.h suffice to access most
  728. // JavaScript APIs. For APIs that are not (sufficiently) exposed, EM_ASM
  729. // is used below.
  730. SET_EM_CALLBACK(EMSCRIPTEN_EVENT_TARGET_WINDOW, mousemove, mousemove_callback)
  731. SET_EM_CALLBACK(id.get_data(), mousedown, mouse_button_callback)
  732. SET_EM_CALLBACK(EMSCRIPTEN_EVENT_TARGET_WINDOW, mouseup, mouse_button_callback)
  733. SET_EM_CALLBACK(id.get_data(), wheel, wheel_callback)
  734. SET_EM_CALLBACK(id.get_data(), touchstart, touch_press_callback)
  735. SET_EM_CALLBACK(id.get_data(), touchmove, touchmove_callback)
  736. SET_EM_CALLBACK(id.get_data(), touchend, touch_press_callback)
  737. SET_EM_CALLBACK(id.get_data(), touchcancel, touch_press_callback)
  738. SET_EM_CALLBACK(id.get_data(), keydown, keydown_callback)
  739. SET_EM_CALLBACK(id.get_data(), keypress, keypress_callback)
  740. SET_EM_CALLBACK(id.get_data(), keyup, keyup_callback)
  741. SET_EM_CALLBACK(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, fullscreenchange, fullscreen_change_callback)
  742. SET_EM_CALLBACK_NOTARGET(gamepadconnected, gamepad_change_callback)
  743. SET_EM_CALLBACK_NOTARGET(gamepaddisconnected, gamepad_change_callback)
  744. #undef SET_EM_CALLBACK_NOTARGET
  745. #undef SET_EM_CALLBACK
  746. #undef EM_CHECK
  747. /* clang-format off */
  748. EM_ASM_ARGS({
  749. Module.listeners = {};
  750. const send_window_event = cwrap('send_window_event', null, ['number']);
  751. const notifications = arguments;
  752. (['mouseover', 'mouseleave', 'focus', 'blur']).forEach(function(event, index) {
  753. Module.listeners[event] = send_window_event.bind(null, notifications[index]);
  754. Module['canvas'].addEventListener(event, Module.listeners[event]);
  755. });
  756. // Clipboard
  757. const update_clipboard = cwrap('update_clipboard', null, ['string']);
  758. Module.listeners['paste'] = function(evt) {
  759. update_clipboard(evt.clipboardData.getData('text'));
  760. };
  761. window.addEventListener('paste', Module.listeners['paste'], false);
  762. },
  763. WINDOW_EVENT_MOUSE_ENTER,
  764. WINDOW_EVENT_MOUSE_EXIT,
  765. WINDOW_EVENT_FOCUS_IN,
  766. WINDOW_EVENT_FOCUS_OUT
  767. );
  768. /* clang-format on */
  769. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
  770. }
  771. DisplayServerJavaScript::~DisplayServerJavaScript() {
  772. EM_ASM({
  773. Object.entries(Module.listeners).forEach(function(kv) {
  774. if (kv[0] == 'paste') {
  775. window.removeEventListener(kv[0], kv[1], true);
  776. } else {
  777. Module['canvas'].removeEventListener(kv[0], kv[1]);
  778. }
  779. });
  780. Module.listeners = {};
  781. });
  782. //emscripten_webgl_commit_frame();
  783. //emscripten_webgl_destroy_context(webgl_ctx);
  784. }
  785. bool DisplayServerJavaScript::has_feature(Feature p_feature) const {
  786. switch (p_feature) {
  787. //case FEATURE_CONSOLE_WINDOW:
  788. //case FEATURE_GLOBAL_MENU:
  789. //case FEATURE_HIDPI:
  790. //case FEATURE_IME:
  791. case FEATURE_ICON:
  792. case FEATURE_CLIPBOARD:
  793. case FEATURE_CURSOR_SHAPE:
  794. case FEATURE_CUSTOM_CURSOR_SHAPE:
  795. case FEATURE_MOUSE:
  796. case FEATURE_TOUCHSCREEN:
  797. return true;
  798. //case FEATURE_MOUSE_WARP:
  799. //case FEATURE_NATIVE_DIALOG:
  800. //case FEATURE_NATIVE_ICON:
  801. //case FEATURE_NATIVE_VIDEO:
  802. //case FEATURE_WINDOW_TRANSPARENCY:
  803. //case FEATURE_KEEP_SCREEN_ON:
  804. //case FEATURE_ORIENTATION:
  805. //case FEATURE_VIRTUAL_KEYBOARD:
  806. default:
  807. return false;
  808. }
  809. }
  810. void DisplayServerJavaScript::register_javascript_driver() {
  811. register_create_function("javascript", create_func, get_rendering_drivers_func);
  812. }
  813. String DisplayServerJavaScript::get_name() const {
  814. return "javascript";
  815. }
  816. int DisplayServerJavaScript::get_screen_count() const {
  817. return 1;
  818. }
  819. Point2i DisplayServerJavaScript::screen_get_position(int p_screen) const {
  820. return Point2i(); // TODO offsetX/Y?
  821. }
  822. Size2i DisplayServerJavaScript::screen_get_size(int p_screen) const {
  823. EmscriptenFullscreenChangeEvent ev;
  824. EMSCRIPTEN_RESULT result = emscripten_get_fullscreen_status(&ev);
  825. ERR_FAIL_COND_V(result != EMSCRIPTEN_RESULT_SUCCESS, Size2i());
  826. return Size2i(ev.screenWidth, ev.screenHeight);
  827. }
  828. Rect2i DisplayServerJavaScript::screen_get_usable_rect(int p_screen) const {
  829. int canvas[2];
  830. emscripten_get_canvas_element_size(canvas_id.utf8().get_data(), canvas, canvas + 1);
  831. return Rect2i(0, 0, canvas[0], canvas[1]);
  832. }
  833. int DisplayServerJavaScript::screen_get_dpi(int p_screen) const {
  834. return 96; // TODO maybe check pixel ratio via window.devicePixelRatio * 96? Inexact.
  835. }
  836. Vector<DisplayServer::WindowID> DisplayServerJavaScript::get_window_list() const {
  837. Vector<WindowID> ret;
  838. ret.push_back(MAIN_WINDOW_ID);
  839. return ret;
  840. }
  841. DisplayServerJavaScript::WindowID DisplayServerJavaScript::get_window_at_screen_position(const Point2i &p_position) const {
  842. return MAIN_WINDOW_ID;
  843. }
  844. void DisplayServerJavaScript::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  845. window_attached_instance_id = p_instance;
  846. }
  847. ObjectID DisplayServerJavaScript::window_get_attached_instance_id(WindowID p_window) const {
  848. return window_attached_instance_id;
  849. }
  850. void DisplayServerJavaScript::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  851. // Not supported.
  852. }
  853. void DisplayServerJavaScript::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  854. window_event_callback = p_callable;
  855. }
  856. void DisplayServerJavaScript::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  857. input_event_callback = p_callable;
  858. }
  859. void DisplayServerJavaScript::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  860. input_text_callback = p_callable; // TODO unused... do I need this?
  861. }
  862. void DisplayServerJavaScript::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  863. // TODO this should be implemented.
  864. }
  865. void DisplayServerJavaScript::window_set_title(const String &p_title, WindowID p_window) {
  866. /* clang-format off */
  867. EM_ASM_({
  868. document.title = UTF8ToString($0);
  869. }, p_title.utf8().get_data());
  870. /* clang-format on */
  871. }
  872. int DisplayServerJavaScript::window_get_current_screen(WindowID p_window) const {
  873. return 1;
  874. }
  875. void DisplayServerJavaScript::window_set_current_screen(int p_screen, WindowID p_window) {
  876. // Not implemented.
  877. }
  878. Point2i DisplayServerJavaScript::window_get_position(WindowID p_window) const {
  879. return Point2i(); // TODO Does this need implementation?
  880. }
  881. void DisplayServerJavaScript::window_set_position(const Point2i &p_position, WindowID p_window) {
  882. // Not supported.
  883. }
  884. void DisplayServerJavaScript::window_set_transient(WindowID p_window, WindowID p_parent) {
  885. // Not supported.
  886. }
  887. void DisplayServerJavaScript::window_set_max_size(const Size2i p_size, WindowID p_window) {
  888. // Not supported.
  889. }
  890. Size2i DisplayServerJavaScript::window_get_max_size(WindowID p_window) const {
  891. return Size2i();
  892. }
  893. void DisplayServerJavaScript::window_set_min_size(const Size2i p_size, WindowID p_window) {
  894. // Not supported.
  895. }
  896. Size2i DisplayServerJavaScript::window_get_min_size(WindowID p_window) const {
  897. return Size2i();
  898. }
  899. void DisplayServerJavaScript::window_set_size(const Size2i p_size, WindowID p_window) {
  900. emscripten_set_canvas_element_size(canvas_id.utf8().get_data(), p_size.x, p_size.y);
  901. }
  902. Size2i DisplayServerJavaScript::window_get_size(WindowID p_window) const {
  903. int canvas[2];
  904. emscripten_get_canvas_element_size(canvas_id.utf8().get_data(), canvas, canvas + 1);
  905. return Size2(canvas[0], canvas[1]);
  906. }
  907. Size2i DisplayServerJavaScript::window_get_real_size(WindowID p_window) const {
  908. return window_get_size(p_window);
  909. }
  910. void DisplayServerJavaScript::window_set_mode(WindowMode p_mode, WindowID p_window) {
  911. if (window_mode == p_mode)
  912. return;
  913. switch (p_mode) {
  914. case WINDOW_MODE_WINDOWED: {
  915. if (window_mode == WINDOW_MODE_FULLSCREEN) {
  916. emscripten_exit_fullscreen();
  917. }
  918. window_mode = WINDOW_MODE_WINDOWED;
  919. window_set_size(windowed_size);
  920. } break;
  921. case WINDOW_MODE_FULLSCREEN: {
  922. EmscriptenFullscreenStrategy strategy;
  923. strategy.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_STRETCH;
  924. strategy.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
  925. strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
  926. strategy.canvasResizedCallback = nullptr;
  927. EMSCRIPTEN_RESULT result = emscripten_request_fullscreen_strategy(canvas_id.utf8().get_data(), false, &strategy);
  928. ERR_FAIL_COND_MSG(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED, "Enabling fullscreen is only possible from an input callback for the HTML5 platform.");
  929. ERR_FAIL_COND_MSG(result != EMSCRIPTEN_RESULT_SUCCESS, "Enabling fullscreen is only possible from an input callback for the HTML5 platform.");
  930. } break;
  931. case WINDOW_MODE_MAXIMIZED:
  932. case WINDOW_MODE_MINIMIZED:
  933. WARN_PRINT("WindowMode MAXIMIZED and MINIMIZED are not supported in HTML5 platform.");
  934. break;
  935. default:
  936. break;
  937. }
  938. }
  939. DisplayServerJavaScript::WindowMode DisplayServerJavaScript::window_get_mode(WindowID p_window) const {
  940. return window_mode;
  941. }
  942. bool DisplayServerJavaScript::window_is_maximize_allowed(WindowID p_window) const {
  943. return false;
  944. }
  945. void DisplayServerJavaScript::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  946. // Not supported.
  947. }
  948. bool DisplayServerJavaScript::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  949. return false;
  950. }
  951. void DisplayServerJavaScript::window_request_attention(WindowID p_window) {
  952. // Not supported.
  953. }
  954. void DisplayServerJavaScript::window_move_to_foreground(WindowID p_window) {
  955. // Not supported.
  956. }
  957. bool DisplayServerJavaScript::window_can_draw(WindowID p_window) const {
  958. return true;
  959. }
  960. bool DisplayServerJavaScript::can_any_window_draw() const {
  961. return true;
  962. }
  963. void DisplayServerJavaScript::process_events() {
  964. if (emscripten_sample_gamepad_data() == EMSCRIPTEN_RESULT_SUCCESS)
  965. process_joypads();
  966. }
  967. int DisplayServerJavaScript::get_current_video_driver() const {
  968. return 1;
  969. }
  970. void DisplayServerJavaScript::swap_buffers() {
  971. //emscripten_webgl_commit_frame();
  972. }