display_server_javascript.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*************************************************************************/
  2. /* display_server_javascript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "display_server_javascript.h"
  31. #ifdef GLES3_ENABLED
  32. #include "drivers/gles3/rasterizer_gles3.h"
  33. #endif
  34. #include "platform/javascript/os_javascript.h"
  35. #include "servers/rendering/dummy/rasterizer_dummy.h"
  36. #include <emscripten.h>
  37. #include <png.h>
  38. #include "dom_keys.inc"
  39. #include "godot_js.h"
  40. #define DOM_BUTTON_LEFT 0
  41. #define DOM_BUTTON_MIDDLE 1
  42. #define DOM_BUTTON_RIGHT 2
  43. #define DOM_BUTTON_XBUTTON1 3
  44. #define DOM_BUTTON_XBUTTON2 4
  45. DisplayServerJavaScript *DisplayServerJavaScript::get_singleton() {
  46. return static_cast<DisplayServerJavaScript *>(DisplayServer::get_singleton());
  47. }
  48. // Window (canvas)
  49. bool DisplayServerJavaScript::check_size_force_redraw() {
  50. return godot_js_display_size_update() != 0;
  51. }
  52. void DisplayServerJavaScript::fullscreen_change_callback(int p_fullscreen) {
  53. DisplayServerJavaScript *display = get_singleton();
  54. if (p_fullscreen) {
  55. display->window_mode = WINDOW_MODE_FULLSCREEN;
  56. } else {
  57. display->window_mode = WINDOW_MODE_WINDOWED;
  58. }
  59. }
  60. // Drag and drop callback.
  61. void DisplayServerJavaScript::drop_files_js_callback(char **p_filev, int p_filec) {
  62. DisplayServerJavaScript *ds = get_singleton();
  63. if (!ds) {
  64. ERR_FAIL_MSG("Unable to drop files because the DisplayServer is not active");
  65. }
  66. if (ds->drop_files_callback.is_null()) {
  67. return;
  68. }
  69. Vector<String> files;
  70. for (int i = 0; i < p_filec; i++) {
  71. files.push_back(String::utf8(p_filev[i]));
  72. }
  73. Variant v = files;
  74. Variant *vp = &v;
  75. Variant ret;
  76. Callable::CallError ce;
  77. ds->drop_files_callback.callp((const Variant **)&vp, 1, ret, ce);
  78. }
  79. // JavaScript quit request callback.
  80. void DisplayServerJavaScript::request_quit_callback() {
  81. DisplayServerJavaScript *ds = get_singleton();
  82. if (ds && !ds->window_event_callback.is_null()) {
  83. Variant event = int(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST);
  84. Variant *eventp = &event;
  85. Variant ret;
  86. Callable::CallError ce;
  87. ds->window_event_callback.callp((const Variant **)&eventp, 1, ret, ce);
  88. }
  89. }
  90. // Keys
  91. void DisplayServerJavaScript::dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod) {
  92. ev->set_shift_pressed(p_mod & 1);
  93. ev->set_alt_pressed(p_mod & 2);
  94. ev->set_ctrl_pressed(p_mod & 4);
  95. ev->set_meta_pressed(p_mod & 8);
  96. }
  97. void DisplayServerJavaScript::key_callback(int p_pressed, int p_repeat, int p_modifiers) {
  98. DisplayServerJavaScript *ds = get_singleton();
  99. JSKeyEvent &key_event = ds->key_event;
  100. // Resume audio context after input in case autoplay was denied.
  101. OS_JavaScript::get_singleton()->resume_audio();
  102. Ref<InputEventKey> ev;
  103. ev.instantiate();
  104. ev->set_echo(p_repeat);
  105. ev->set_keycode(dom_code2godot_scancode(key_event.code, key_event.key, false));
  106. ev->set_physical_keycode(dom_code2godot_scancode(key_event.code, key_event.key, true));
  107. ev->set_pressed(p_pressed);
  108. dom2godot_mod(ev, p_modifiers);
  109. String unicode = String::utf8(key_event.key);
  110. if (unicode.length() == 1) {
  111. ev->set_unicode(unicode[0]);
  112. }
  113. Input::get_singleton()->parse_input_event(ev);
  114. // Make sure to flush all events so we can call restricted APIs inside the event.
  115. Input::get_singleton()->flush_buffered_events();
  116. }
  117. // Mouse
  118. int DisplayServerJavaScript::mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) {
  119. DisplayServerJavaScript *ds = get_singleton();
  120. Point2 pos(p_x, p_y);
  121. Ref<InputEventMouseButton> ev;
  122. ev.instantiate();
  123. ev->set_position(pos);
  124. ev->set_global_position(pos);
  125. ev->set_pressed(p_pressed);
  126. dom2godot_mod(ev, p_modifiers);
  127. switch (p_button) {
  128. case DOM_BUTTON_LEFT:
  129. ev->set_button_index(MouseButton::LEFT);
  130. break;
  131. case DOM_BUTTON_MIDDLE:
  132. ev->set_button_index(MouseButton::MIDDLE);
  133. break;
  134. case DOM_BUTTON_RIGHT:
  135. ev->set_button_index(MouseButton::RIGHT);
  136. break;
  137. case DOM_BUTTON_XBUTTON1:
  138. ev->set_button_index(MouseButton::MB_XBUTTON1);
  139. break;
  140. case DOM_BUTTON_XBUTTON2:
  141. ev->set_button_index(MouseButton::MB_XBUTTON2);
  142. break;
  143. default:
  144. return false;
  145. }
  146. if (p_pressed) {
  147. uint64_t diff = (OS::get_singleton()->get_ticks_usec() / 1000) - ds->last_click_ms;
  148. if (ev->get_button_index() == ds->last_click_button_index) {
  149. if (diff < 400 && Point2(ds->last_click_pos).distance_to(ev->get_position()) < 5) {
  150. ds->last_click_ms = 0;
  151. ds->last_click_pos = Point2(-100, -100);
  152. ds->last_click_button_index = MouseButton::NONE;
  153. ev->set_double_click(true);
  154. }
  155. } else {
  156. ds->last_click_button_index = ev->get_button_index();
  157. }
  158. if (!ev->is_double_click()) {
  159. ds->last_click_ms += diff;
  160. ds->last_click_pos = ev->get_position();
  161. }
  162. }
  163. MouseButton mask = Input::get_singleton()->get_mouse_button_mask();
  164. MouseButton button_flag = mouse_button_to_mask(ev->get_button_index());
  165. if (ev->is_pressed()) {
  166. mask |= button_flag;
  167. } else if ((mask & button_flag) != MouseButton::NONE) {
  168. mask &= ~button_flag;
  169. } else {
  170. // Received release event, but press was outside the canvas, so ignore.
  171. return false;
  172. }
  173. ev->set_button_mask(mask);
  174. Input::get_singleton()->parse_input_event(ev);
  175. // Resume audio context after input in case autoplay was denied.
  176. OS_JavaScript::get_singleton()->resume_audio();
  177. // Make sure to flush all events so we can call restricted APIs inside the event.
  178. Input::get_singleton()->flush_buffered_events();
  179. // Prevent multi-click text selection and wheel-click scrolling anchor.
  180. // Context menu is prevented through contextmenu event.
  181. return true;
  182. }
  183. void DisplayServerJavaScript::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
  184. MouseButton input_mask = Input::get_singleton()->get_mouse_button_mask();
  185. // For motion outside the canvas, only read mouse movement if dragging
  186. // started inside the canvas; imitating desktop app behaviour.
  187. if (!get_singleton()->cursor_inside_canvas && input_mask == MouseButton::NONE) {
  188. return;
  189. }
  190. Point2 pos(p_x, p_y);
  191. Ref<InputEventMouseMotion> ev;
  192. ev.instantiate();
  193. dom2godot_mod(ev, p_modifiers);
  194. ev->set_button_mask(input_mask);
  195. ev->set_position(pos);
  196. ev->set_global_position(pos);
  197. ev->set_relative(Vector2(p_rel_x, p_rel_y));
  198. ev->set_velocity(Input::get_singleton()->get_last_mouse_velocity());
  199. Input::get_singleton()->parse_input_event(ev);
  200. }
  201. // Cursor
  202. const char *DisplayServerJavaScript::godot2dom_cursor(DisplayServer::CursorShape p_shape) {
  203. switch (p_shape) {
  204. case DisplayServer::CURSOR_ARROW:
  205. return "default";
  206. case DisplayServer::CURSOR_IBEAM:
  207. return "text";
  208. case DisplayServer::CURSOR_POINTING_HAND:
  209. return "pointer";
  210. case DisplayServer::CURSOR_CROSS:
  211. return "crosshair";
  212. case DisplayServer::CURSOR_WAIT:
  213. return "wait";
  214. case DisplayServer::CURSOR_BUSY:
  215. return "progress";
  216. case DisplayServer::CURSOR_DRAG:
  217. return "grab";
  218. case DisplayServer::CURSOR_CAN_DROP:
  219. return "grabbing";
  220. case DisplayServer::CURSOR_FORBIDDEN:
  221. return "no-drop";
  222. case DisplayServer::CURSOR_VSIZE:
  223. return "ns-resize";
  224. case DisplayServer::CURSOR_HSIZE:
  225. return "ew-resize";
  226. case DisplayServer::CURSOR_BDIAGSIZE:
  227. return "nesw-resize";
  228. case DisplayServer::CURSOR_FDIAGSIZE:
  229. return "nwse-resize";
  230. case DisplayServer::CURSOR_MOVE:
  231. return "move";
  232. case DisplayServer::CURSOR_VSPLIT:
  233. return "row-resize";
  234. case DisplayServer::CURSOR_HSPLIT:
  235. return "col-resize";
  236. case DisplayServer::CURSOR_HELP:
  237. return "help";
  238. default:
  239. return "default";
  240. }
  241. }
  242. bool DisplayServerJavaScript::tts_is_speaking() const {
  243. return godot_js_tts_is_speaking();
  244. }
  245. bool DisplayServerJavaScript::tts_is_paused() const {
  246. return godot_js_tts_is_paused();
  247. }
  248. void DisplayServerJavaScript::update_voices_callback(int p_size, const char **p_voice) {
  249. get_singleton()->voices.clear();
  250. for (int i = 0; i < p_size; i++) {
  251. Vector<String> tokens = String::utf8(p_voice[i]).split(";", true, 2);
  252. if (tokens.size() == 2) {
  253. Dictionary voice_d;
  254. voice_d["name"] = tokens[1];
  255. voice_d["id"] = tokens[1];
  256. voice_d["language"] = tokens[0];
  257. get_singleton()->voices.push_back(voice_d);
  258. }
  259. }
  260. }
  261. TypedArray<Dictionary> DisplayServerJavaScript::tts_get_voices() const {
  262. godot_js_tts_get_voices(update_voices_callback);
  263. return voices;
  264. }
  265. void DisplayServerJavaScript::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
  266. if (p_interrupt) {
  267. tts_stop();
  268. }
  269. if (p_text.is_empty()) {
  270. tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, p_utterance_id);
  271. return;
  272. }
  273. CharString string = p_text.utf8();
  274. utterance_ids[p_utterance_id] = string;
  275. godot_js_tts_speak(string.get_data(), p_voice.utf8().get_data(), CLAMP(p_volume, 0, 100), CLAMP(p_pitch, 0.f, 2.f), CLAMP(p_rate, 0.1f, 10.f), p_utterance_id, DisplayServerJavaScript::_js_utterance_callback);
  276. }
  277. void DisplayServerJavaScript::tts_pause() {
  278. godot_js_tts_pause();
  279. }
  280. void DisplayServerJavaScript::tts_resume() {
  281. godot_js_tts_resume();
  282. }
  283. void DisplayServerJavaScript::tts_stop() {
  284. for (const KeyValue<int, CharString> &E : utterance_ids) {
  285. tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, E.key);
  286. }
  287. utterance_ids.clear();
  288. godot_js_tts_stop();
  289. }
  290. void DisplayServerJavaScript::_js_utterance_callback(int p_event, int p_id, int p_pos) {
  291. DisplayServerJavaScript *ds = (DisplayServerJavaScript *)DisplayServer::get_singleton();
  292. if (ds->utterance_ids.has(p_id)) {
  293. int pos = 0;
  294. if ((TTSUtteranceEvent)p_event == DisplayServer::TTS_UTTERANCE_BOUNDARY) {
  295. // Convert position from UTF-8 to UTF-32.
  296. const CharString &string = ds->utterance_ids[p_id];
  297. for (int i = 0; i < MIN(p_pos, string.length()); i++) {
  298. uint8_t c = string[i];
  299. if ((c & 0xe0) == 0xc0) {
  300. i += 1;
  301. } else if ((c & 0xf0) == 0xe0) {
  302. i += 2;
  303. } else if ((c & 0xf8) == 0xf0) {
  304. i += 3;
  305. }
  306. pos++;
  307. }
  308. } else if ((TTSUtteranceEvent)p_event != DisplayServer::TTS_UTTERANCE_STARTED) {
  309. ds->utterance_ids.erase(p_id);
  310. }
  311. ds->tts_post_utterance_event((TTSUtteranceEvent)p_event, p_id, pos);
  312. }
  313. }
  314. void DisplayServerJavaScript::cursor_set_shape(CursorShape p_shape) {
  315. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  316. if (cursor_shape == p_shape) {
  317. return;
  318. }
  319. cursor_shape = p_shape;
  320. godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape));
  321. }
  322. DisplayServer::CursorShape DisplayServerJavaScript::cursor_get_shape() const {
  323. return cursor_shape;
  324. }
  325. void DisplayServerJavaScript::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  326. if (p_cursor.is_valid()) {
  327. Ref<Texture2D> texture = p_cursor;
  328. Ref<AtlasTexture> atlas_texture = p_cursor;
  329. Ref<Image> image;
  330. Size2 texture_size;
  331. Rect2 atlas_rect;
  332. if (texture.is_valid()) {
  333. image = texture->get_image();
  334. }
  335. if (!image.is_valid() && atlas_texture.is_valid()) {
  336. texture = atlas_texture->get_atlas();
  337. atlas_rect.size.width = texture->get_width();
  338. atlas_rect.size.height = texture->get_height();
  339. atlas_rect.position.x = atlas_texture->get_region().position.x;
  340. atlas_rect.position.y = atlas_texture->get_region().position.y;
  341. texture_size.width = atlas_texture->get_region().size.x;
  342. texture_size.height = atlas_texture->get_region().size.y;
  343. } else if (image.is_valid()) {
  344. texture_size.width = texture->get_width();
  345. texture_size.height = texture->get_height();
  346. }
  347. ERR_FAIL_COND(!texture.is_valid());
  348. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  349. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  350. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  351. image = texture->get_image();
  352. ERR_FAIL_COND(!image.is_valid());
  353. image = image->duplicate();
  354. if (atlas_texture.is_valid()) {
  355. image->crop_from_point(
  356. atlas_rect.position.x,
  357. atlas_rect.position.y,
  358. texture_size.width,
  359. texture_size.height);
  360. }
  361. if (image->get_format() != Image::FORMAT_RGBA8) {
  362. image->convert(Image::FORMAT_RGBA8);
  363. }
  364. png_image png_meta;
  365. memset(&png_meta, 0, sizeof png_meta);
  366. png_meta.version = PNG_IMAGE_VERSION;
  367. png_meta.width = texture_size.width;
  368. png_meta.height = texture_size.height;
  369. png_meta.format = PNG_FORMAT_RGBA;
  370. PackedByteArray png;
  371. size_t len;
  372. PackedByteArray data = image->get_data();
  373. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  374. png.resize(len);
  375. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  376. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), png.ptr(), len, p_hotspot.x, p_hotspot.y);
  377. } else {
  378. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), nullptr, 0, 0, 0);
  379. }
  380. cursor_set_shape(cursor_shape);
  381. }
  382. // Mouse mode
  383. void DisplayServerJavaScript::mouse_set_mode(MouseMode p_mode) {
  384. ERR_FAIL_COND_MSG(p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN, "MOUSE_MODE_CONFINED is not supported for the HTML5 platform.");
  385. if (p_mode == mouse_get_mode()) {
  386. return;
  387. }
  388. if (p_mode == MOUSE_MODE_VISIBLE) {
  389. godot_js_display_cursor_set_visible(1);
  390. godot_js_display_cursor_lock_set(0);
  391. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  392. godot_js_display_cursor_set_visible(0);
  393. godot_js_display_cursor_lock_set(0);
  394. } else if (p_mode == MOUSE_MODE_CAPTURED) {
  395. godot_js_display_cursor_set_visible(1);
  396. godot_js_display_cursor_lock_set(1);
  397. }
  398. }
  399. DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const {
  400. if (godot_js_display_cursor_is_hidden()) {
  401. return MOUSE_MODE_HIDDEN;
  402. }
  403. if (godot_js_display_cursor_is_locked()) {
  404. return MOUSE_MODE_CAPTURED;
  405. }
  406. return MOUSE_MODE_VISIBLE;
  407. }
  408. Point2i DisplayServerJavaScript::mouse_get_position() const {
  409. return Input::get_singleton()->get_mouse_position();
  410. }
  411. // Wheel
  412. int DisplayServerJavaScript::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
  413. if (!godot_js_display_canvas_is_focused()) {
  414. if (get_singleton()->cursor_inside_canvas) {
  415. godot_js_display_canvas_focus();
  416. } else {
  417. return false;
  418. }
  419. }
  420. Input *input = Input::get_singleton();
  421. Ref<InputEventMouseButton> ev;
  422. ev.instantiate();
  423. ev->set_position(input->get_mouse_position());
  424. ev->set_global_position(ev->get_position());
  425. ev->set_shift_pressed(input->is_key_pressed(Key::SHIFT));
  426. ev->set_alt_pressed(input->is_key_pressed(Key::ALT));
  427. ev->set_ctrl_pressed(input->is_key_pressed(Key::CTRL));
  428. ev->set_meta_pressed(input->is_key_pressed(Key::META));
  429. if (p_delta_y < 0) {
  430. ev->set_button_index(MouseButton::WHEEL_UP);
  431. } else if (p_delta_y > 0) {
  432. ev->set_button_index(MouseButton::WHEEL_DOWN);
  433. } else if (p_delta_x > 0) {
  434. ev->set_button_index(MouseButton::WHEEL_LEFT);
  435. } else if (p_delta_x < 0) {
  436. ev->set_button_index(MouseButton::WHEEL_RIGHT);
  437. } else {
  438. return false;
  439. }
  440. // Different browsers give wildly different delta values, and we can't
  441. // interpret deltaMode, so use default value for wheel events' factor.
  442. MouseButton button_flag = mouse_button_to_mask(ev->get_button_index());
  443. ev->set_pressed(true);
  444. ev->set_button_mask(input->get_mouse_button_mask() | button_flag);
  445. input->parse_input_event(ev);
  446. Ref<InputEventMouseButton> release = ev->duplicate();
  447. release->set_pressed(false);
  448. release->set_button_mask(MouseButton(input->get_mouse_button_mask() & ~button_flag));
  449. input->parse_input_event(release);
  450. return true;
  451. }
  452. // Touch
  453. void DisplayServerJavaScript::touch_callback(int p_type, int p_count) {
  454. DisplayServerJavaScript *ds = get_singleton();
  455. const JSTouchEvent &touch_event = ds->touch_event;
  456. for (int i = 0; i < p_count; i++) {
  457. Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]);
  458. if (p_type == 2) {
  459. // touchmove
  460. Ref<InputEventScreenDrag> ev;
  461. ev.instantiate();
  462. ev->set_index(touch_event.identifier[i]);
  463. ev->set_position(point);
  464. Point2 &prev = ds->touches[i];
  465. ev->set_relative(ev->get_position() - prev);
  466. prev = ev->get_position();
  467. Input::get_singleton()->parse_input_event(ev);
  468. } else {
  469. // touchstart/touchend
  470. Ref<InputEventScreenTouch> ev;
  471. // Resume audio context after input in case autoplay was denied.
  472. OS_JavaScript::get_singleton()->resume_audio();
  473. ev.instantiate();
  474. ev->set_index(touch_event.identifier[i]);
  475. ev->set_position(point);
  476. ev->set_pressed(p_type == 0);
  477. ds->touches[i] = point;
  478. Input::get_singleton()->parse_input_event(ev);
  479. // Make sure to flush all events so we can call restricted APIs inside the event.
  480. Input::get_singleton()->flush_buffered_events();
  481. }
  482. }
  483. }
  484. bool DisplayServerJavaScript::screen_is_touchscreen(int p_screen) const {
  485. return godot_js_display_touchscreen_is_available();
  486. }
  487. // Virtual Keyboard
  488. void DisplayServerJavaScript::vk_input_text_callback(const char *p_text, int p_cursor) {
  489. DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton();
  490. if (!ds || ds->input_text_callback.is_null()) {
  491. return;
  492. }
  493. // Call input_text
  494. Variant event = String::utf8(p_text);
  495. Variant *eventp = &event;
  496. Variant ret;
  497. Callable::CallError ce;
  498. ds->input_text_callback.callp((const Variant **)&eventp, 1, ret, ce);
  499. // Insert key right to reach position.
  500. Input *input = Input::get_singleton();
  501. Ref<InputEventKey> k;
  502. for (int i = 0; i < p_cursor; i++) {
  503. k.instantiate();
  504. k->set_pressed(true);
  505. k->set_echo(false);
  506. k->set_keycode(Key::RIGHT);
  507. input->parse_input_event(k);
  508. k.instantiate();
  509. k->set_pressed(false);
  510. k->set_echo(false);
  511. k->set_keycode(Key::RIGHT);
  512. input->parse_input_event(k);
  513. }
  514. }
  515. void DisplayServerJavaScript::virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect, VirtualKeyboardType p_type, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  516. godot_js_display_vk_show(p_existing_text.utf8().get_data(), p_type, p_cursor_start, p_cursor_end);
  517. }
  518. void DisplayServerJavaScript::virtual_keyboard_hide() {
  519. godot_js_display_vk_hide();
  520. }
  521. void DisplayServerJavaScript::window_blur_callback() {
  522. Input::get_singleton()->release_pressed_events();
  523. }
  524. // Gamepad
  525. void DisplayServerJavaScript::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) {
  526. Input *input = Input::get_singleton();
  527. if (p_connected) {
  528. input->joy_connection_changed(p_index, true, String::utf8(p_id), String::utf8(p_guid));
  529. } else {
  530. input->joy_connection_changed(p_index, false, "");
  531. }
  532. }
  533. void DisplayServerJavaScript::process_joypads() {
  534. Input *input = Input::get_singleton();
  535. int32_t pads = godot_js_input_gamepad_sample_count();
  536. int32_t s_btns_num = 0;
  537. int32_t s_axes_num = 0;
  538. int32_t s_standard = 0;
  539. float s_btns[16];
  540. float s_axes[10];
  541. for (int idx = 0; idx < pads; idx++) {
  542. int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
  543. if (err) {
  544. continue;
  545. }
  546. for (int b = 0; b < s_btns_num; b++) {
  547. // Buttons 6 and 7 in the standard mapping need to be
  548. // axis to be handled as JoyAxis::TRIGGER by Godot.
  549. if (s_standard && (b == 6 || b == 7)) {
  550. input->joy_axis(idx, (JoyAxis)b, s_btns[b]);
  551. } else {
  552. input->joy_button(idx, (JoyButton)b, s_btns[b]);
  553. }
  554. }
  555. for (int a = 0; a < s_axes_num; a++) {
  556. input->joy_axis(idx, (JoyAxis)a, s_axes[a]);
  557. }
  558. }
  559. }
  560. Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() {
  561. Vector<String> drivers;
  562. #ifdef GLES3_ENABLED
  563. drivers.push_back("opengl3");
  564. #endif
  565. return drivers;
  566. }
  567. // Clipboard
  568. void DisplayServerJavaScript::update_clipboard_callback(const char *p_text) {
  569. get_singleton()->clipboard = String::utf8(p_text);
  570. }
  571. void DisplayServerJavaScript::clipboard_set(const String &p_text) {
  572. clipboard = p_text;
  573. int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
  574. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  575. }
  576. String DisplayServerJavaScript::clipboard_get() const {
  577. godot_js_display_clipboard_get(update_clipboard_callback);
  578. return clipboard;
  579. }
  580. void DisplayServerJavaScript::send_window_event_callback(int p_notification) {
  581. DisplayServerJavaScript *ds = get_singleton();
  582. if (!ds) {
  583. return;
  584. }
  585. if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
  586. ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  587. }
  588. if (!ds->window_event_callback.is_null()) {
  589. Variant event = int(p_notification);
  590. Variant *eventp = &event;
  591. Variant ret;
  592. Callable::CallError ce;
  593. ds->window_event_callback.callp((const Variant **)&eventp, 1, ret, ce);
  594. }
  595. }
  596. void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) {
  597. ERR_FAIL_COND(p_icon.is_null());
  598. Ref<Image> icon = p_icon;
  599. if (icon->is_compressed()) {
  600. icon = icon->duplicate();
  601. ERR_FAIL_COND(icon->decompress() != OK);
  602. }
  603. if (icon->get_format() != Image::FORMAT_RGBA8) {
  604. if (icon == p_icon) {
  605. icon = icon->duplicate();
  606. }
  607. icon->convert(Image::FORMAT_RGBA8);
  608. }
  609. png_image png_meta;
  610. memset(&png_meta, 0, sizeof png_meta);
  611. png_meta.version = PNG_IMAGE_VERSION;
  612. png_meta.width = icon->get_width();
  613. png_meta.height = icon->get_height();
  614. png_meta.format = PNG_FORMAT_RGBA;
  615. PackedByteArray png;
  616. size_t len;
  617. PackedByteArray data = icon->get_data();
  618. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  619. png.resize(len);
  620. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  621. godot_js_display_window_icon_set(png.ptr(), len);
  622. }
  623. void DisplayServerJavaScript::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  624. Callable cb = get_singleton()->input_event_callback;
  625. if (!cb.is_null()) {
  626. Variant ev = p_event;
  627. Variant *evp = &ev;
  628. Variant ret;
  629. Callable::CallError ce;
  630. cb.callp((const Variant **)&evp, 1, ret, ce);
  631. }
  632. }
  633. DisplayServer *DisplayServerJavaScript::create_func(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Size2i &p_resolution, Error &r_error) {
  634. return memnew(DisplayServerJavaScript(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  635. }
  636. DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_driver, WindowMode p_window_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Size2i &p_resolution, Error &r_error) {
  637. r_error = OK; // Always succeeds for now.
  638. // Ensure the canvas ID.
  639. godot_js_config_canvas_id_get(canvas_id, 256);
  640. // Handle contextmenu, webglcontextlost
  641. godot_js_display_setup_canvas(p_resolution.x, p_resolution.y, (p_window_mode == WINDOW_MODE_FULLSCREEN || p_window_mode == WINDOW_MODE_EXCLUSIVE_FULLSCREEN), OS::get_singleton()->is_hidpi_allowed() ? 1 : 0);
  642. // Check if it's windows.
  643. swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1;
  644. // Expose method for requesting quit.
  645. godot_js_os_request_quit_cb(request_quit_callback);
  646. #ifdef GLES3_ENABLED
  647. // TODO "vulkan" defaults to webgl2 for now.
  648. bool wants_webgl2 = p_rendering_driver == "opengl3" || p_rendering_driver == "vulkan";
  649. bool webgl2_init_failed = wants_webgl2 && !godot_js_display_has_webgl(2);
  650. if (wants_webgl2 && !webgl2_init_failed) {
  651. EmscriptenWebGLContextAttributes attributes;
  652. emscripten_webgl_init_context_attributes(&attributes);
  653. //attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
  654. attributes.alpha = true;
  655. attributes.antialias = false;
  656. attributes.majorVersion = 2;
  657. webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes);
  658. if (emscripten_webgl_make_context_current(webgl_ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
  659. webgl2_init_failed = true;
  660. } else {
  661. RasterizerGLES3::make_current();
  662. }
  663. }
  664. if (webgl2_init_failed) {
  665. OS::get_singleton()->alert("Your browser does not seem to support WebGL2. Please update your browser version.",
  666. "Unable to initialize video driver");
  667. }
  668. if (!wants_webgl2 || webgl2_init_failed) {
  669. RasterizerDummy::make_current();
  670. }
  671. #else
  672. RasterizerDummy::make_current();
  673. #endif
  674. // JS Input interface (js/libs/library_godot_input.js)
  675. godot_js_input_mouse_button_cb(&DisplayServerJavaScript::mouse_button_callback);
  676. godot_js_input_mouse_move_cb(&DisplayServerJavaScript::mouse_move_callback);
  677. godot_js_input_mouse_wheel_cb(&DisplayServerJavaScript::mouse_wheel_callback);
  678. godot_js_input_touch_cb(&DisplayServerJavaScript::touch_callback, touch_event.identifier, touch_event.coords);
  679. godot_js_input_key_cb(&DisplayServerJavaScript::key_callback, key_event.code, key_event.key);
  680. godot_js_input_paste_cb(update_clipboard_callback);
  681. godot_js_input_drop_files_cb(drop_files_js_callback);
  682. godot_js_input_gamepad_cb(&DisplayServerJavaScript::gamepad_callback);
  683. // JS Display interface (js/libs/library_godot_display.js)
  684. godot_js_display_fullscreen_cb(&DisplayServerJavaScript::fullscreen_change_callback);
  685. godot_js_display_window_blur_cb(&window_blur_callback);
  686. godot_js_display_notification_cb(&send_window_event_callback,
  687. WINDOW_EVENT_MOUSE_ENTER,
  688. WINDOW_EVENT_MOUSE_EXIT,
  689. WINDOW_EVENT_FOCUS_IN,
  690. WINDOW_EVENT_FOCUS_OUT);
  691. godot_js_display_vk_cb(&vk_input_text_callback);
  692. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
  693. }
  694. DisplayServerJavaScript::~DisplayServerJavaScript() {
  695. #ifdef GLES3_ENABLED
  696. if (webgl_ctx) {
  697. emscripten_webgl_commit_frame();
  698. emscripten_webgl_destroy_context(webgl_ctx);
  699. }
  700. #endif
  701. }
  702. bool DisplayServerJavaScript::has_feature(Feature p_feature) const {
  703. switch (p_feature) {
  704. //case FEATURE_GLOBAL_MENU:
  705. //case FEATURE_HIDPI:
  706. //case FEATURE_IME:
  707. case FEATURE_ICON:
  708. case FEATURE_CLIPBOARD:
  709. case FEATURE_CURSOR_SHAPE:
  710. case FEATURE_CUSTOM_CURSOR_SHAPE:
  711. case FEATURE_MOUSE:
  712. case FEATURE_TOUCHSCREEN:
  713. return true;
  714. //case FEATURE_MOUSE_WARP:
  715. //case FEATURE_NATIVE_DIALOG:
  716. //case FEATURE_NATIVE_ICON:
  717. //case FEATURE_WINDOW_TRANSPARENCY:
  718. //case FEATURE_KEEP_SCREEN_ON:
  719. //case FEATURE_ORIENTATION:
  720. case FEATURE_VIRTUAL_KEYBOARD:
  721. return godot_js_display_vk_available() != 0;
  722. case FEATURE_TEXT_TO_SPEECH:
  723. return godot_js_display_tts_available() != 0;
  724. default:
  725. return false;
  726. }
  727. }
  728. void DisplayServerJavaScript::register_javascript_driver() {
  729. register_create_function("javascript", create_func, get_rendering_drivers_func);
  730. }
  731. String DisplayServerJavaScript::get_name() const {
  732. return "javascript";
  733. }
  734. int DisplayServerJavaScript::get_screen_count() const {
  735. return 1;
  736. }
  737. Point2i DisplayServerJavaScript::screen_get_position(int p_screen) const {
  738. return Point2i(); // TODO offsetX/Y?
  739. }
  740. Size2i DisplayServerJavaScript::screen_get_size(int p_screen) const {
  741. int size[2];
  742. godot_js_display_screen_size_get(size, size + 1);
  743. return Size2(size[0], size[1]);
  744. }
  745. Rect2i DisplayServerJavaScript::screen_get_usable_rect(int p_screen) const {
  746. int size[2];
  747. godot_js_display_window_size_get(size, size + 1);
  748. return Rect2i(0, 0, size[0], size[1]);
  749. }
  750. int DisplayServerJavaScript::screen_get_dpi(int p_screen) const {
  751. return godot_js_display_screen_dpi_get();
  752. }
  753. float DisplayServerJavaScript::screen_get_scale(int p_screen) const {
  754. return godot_js_display_pixel_ratio_get();
  755. }
  756. float DisplayServerJavaScript::screen_get_refresh_rate(int p_screen) const {
  757. return SCREEN_REFRESH_RATE_FALLBACK; // Javascript doesn't have much of a need for the screen refresh rate, and there's no native way to do so.
  758. }
  759. Vector<DisplayServer::WindowID> DisplayServerJavaScript::get_window_list() const {
  760. Vector<WindowID> ret;
  761. ret.push_back(MAIN_WINDOW_ID);
  762. return ret;
  763. }
  764. DisplayServerJavaScript::WindowID DisplayServerJavaScript::get_window_at_screen_position(const Point2i &p_position) const {
  765. return MAIN_WINDOW_ID;
  766. }
  767. void DisplayServerJavaScript::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  768. window_attached_instance_id = p_instance;
  769. }
  770. ObjectID DisplayServerJavaScript::window_get_attached_instance_id(WindowID p_window) const {
  771. return window_attached_instance_id;
  772. }
  773. void DisplayServerJavaScript::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  774. // Not supported.
  775. }
  776. void DisplayServerJavaScript::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  777. window_event_callback = p_callable;
  778. }
  779. void DisplayServerJavaScript::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  780. input_event_callback = p_callable;
  781. }
  782. void DisplayServerJavaScript::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  783. input_text_callback = p_callable;
  784. }
  785. void DisplayServerJavaScript::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  786. drop_files_callback = p_callable;
  787. }
  788. void DisplayServerJavaScript::window_set_title(const String &p_title, WindowID p_window) {
  789. godot_js_display_window_title_set(p_title.utf8().get_data());
  790. }
  791. int DisplayServerJavaScript::window_get_current_screen(WindowID p_window) const {
  792. return 1;
  793. }
  794. void DisplayServerJavaScript::window_set_current_screen(int p_screen, WindowID p_window) {
  795. // Not implemented.
  796. }
  797. Point2i DisplayServerJavaScript::window_get_position(WindowID p_window) const {
  798. return Point2i(); // TODO Does this need implementation?
  799. }
  800. void DisplayServerJavaScript::window_set_position(const Point2i &p_position, WindowID p_window) {
  801. // Not supported.
  802. }
  803. void DisplayServerJavaScript::window_set_transient(WindowID p_window, WindowID p_parent) {
  804. // Not supported.
  805. }
  806. void DisplayServerJavaScript::window_set_max_size(const Size2i p_size, WindowID p_window) {
  807. // Not supported.
  808. }
  809. Size2i DisplayServerJavaScript::window_get_max_size(WindowID p_window) const {
  810. return Size2i();
  811. }
  812. void DisplayServerJavaScript::window_set_min_size(const Size2i p_size, WindowID p_window) {
  813. // Not supported.
  814. }
  815. Size2i DisplayServerJavaScript::window_get_min_size(WindowID p_window) const {
  816. return Size2i();
  817. }
  818. void DisplayServerJavaScript::window_set_size(const Size2i p_size, WindowID p_window) {
  819. godot_js_display_desired_size_set(p_size.x, p_size.y);
  820. }
  821. Size2i DisplayServerJavaScript::window_get_size(WindowID p_window) const {
  822. int size[2];
  823. godot_js_display_window_size_get(size, size + 1);
  824. return Size2i(size[0], size[1]);
  825. }
  826. Size2i DisplayServerJavaScript::window_get_real_size(WindowID p_window) const {
  827. return window_get_size(p_window);
  828. }
  829. void DisplayServerJavaScript::window_set_mode(WindowMode p_mode, WindowID p_window) {
  830. if (window_mode == p_mode) {
  831. return;
  832. }
  833. switch (p_mode) {
  834. case WINDOW_MODE_WINDOWED: {
  835. if (window_mode == WINDOW_MODE_FULLSCREEN) {
  836. godot_js_display_fullscreen_exit();
  837. }
  838. window_mode = WINDOW_MODE_WINDOWED;
  839. } break;
  840. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  841. case WINDOW_MODE_FULLSCREEN: {
  842. int result = godot_js_display_fullscreen_request();
  843. ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
  844. } break;
  845. case WINDOW_MODE_MAXIMIZED:
  846. case WINDOW_MODE_MINIMIZED:
  847. WARN_PRINT("WindowMode MAXIMIZED and MINIMIZED are not supported in HTML5 platform.");
  848. break;
  849. default:
  850. break;
  851. }
  852. }
  853. DisplayServerJavaScript::WindowMode DisplayServerJavaScript::window_get_mode(WindowID p_window) const {
  854. return window_mode;
  855. }
  856. bool DisplayServerJavaScript::window_is_maximize_allowed(WindowID p_window) const {
  857. return false;
  858. }
  859. void DisplayServerJavaScript::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  860. // Not supported.
  861. }
  862. bool DisplayServerJavaScript::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  863. return false;
  864. }
  865. void DisplayServerJavaScript::window_request_attention(WindowID p_window) {
  866. // Not supported.
  867. }
  868. void DisplayServerJavaScript::window_move_to_foreground(WindowID p_window) {
  869. // Not supported.
  870. }
  871. bool DisplayServerJavaScript::window_can_draw(WindowID p_window) const {
  872. return true;
  873. }
  874. bool DisplayServerJavaScript::can_any_window_draw() const {
  875. return true;
  876. }
  877. void DisplayServerJavaScript::process_events() {
  878. Input::get_singleton()->flush_buffered_events();
  879. if (godot_js_input_gamepad_sample() == OK) {
  880. process_joypads();
  881. }
  882. }
  883. int DisplayServerJavaScript::get_current_video_driver() const {
  884. return 1;
  885. }
  886. bool DisplayServerJavaScript::get_swap_cancel_ok() {
  887. return swap_cancel_ok;
  888. }
  889. void DisplayServerJavaScript::swap_buffers() {
  890. #ifdef GLES3_ENABLED
  891. if (webgl_ctx) {
  892. emscripten_webgl_commit_frame();
  893. }
  894. #endif
  895. }