display_server_javascript.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "platform/javascript/display_server_javascript.h"
  31. #include "platform/javascript/os_javascript.h"
  32. #include "servers/rendering/rasterizer_dummy.h"
  33. #include <emscripten.h>
  34. #include <png.h>
  35. #include "dom_keys.inc"
  36. #include "godot_js.h"
  37. #define DOM_BUTTON_LEFT 0
  38. #define DOM_BUTTON_MIDDLE 1
  39. #define DOM_BUTTON_RIGHT 2
  40. #define DOM_BUTTON_XBUTTON1 3
  41. #define DOM_BUTTON_XBUTTON2 4
  42. DisplayServerJavaScript *DisplayServerJavaScript::get_singleton() {
  43. return static_cast<DisplayServerJavaScript *>(DisplayServer::get_singleton());
  44. }
  45. // Window (canvas)
  46. void DisplayServerJavaScript::focus_canvas() {
  47. godot_js_display_canvas_focus();
  48. }
  49. bool DisplayServerJavaScript::is_canvas_focused() {
  50. return godot_js_display_canvas_is_focused() != 0;
  51. }
  52. bool DisplayServerJavaScript::check_size_force_redraw() {
  53. return godot_js_display_size_update() != 0;
  54. }
  55. void DisplayServerJavaScript::fullscreen_change_callback(int p_fullscreen) {
  56. DisplayServerJavaScript *display = get_singleton();
  57. if (p_fullscreen) {
  58. display->window_mode = WINDOW_MODE_FULLSCREEN;
  59. } else {
  60. display->window_mode = WINDOW_MODE_WINDOWED;
  61. }
  62. }
  63. // Drag and drop callback.
  64. void DisplayServerJavaScript::drop_files_js_callback(char **p_filev, int p_filec) {
  65. DisplayServerJavaScript *ds = get_singleton();
  66. if (!ds) {
  67. ERR_FAIL_MSG("Unable to drop files because the DisplayServer is not active");
  68. }
  69. if (ds->drop_files_callback.is_null()) {
  70. return;
  71. }
  72. Vector<String> files;
  73. for (int i = 0; i < p_filec; i++) {
  74. files.push_back(String::utf8(p_filev[i]));
  75. }
  76. Variant v = files;
  77. Variant *vp = &v;
  78. Variant ret;
  79. Callable::CallError ce;
  80. ds->drop_files_callback.call((const Variant **)&vp, 1, ret, ce);
  81. }
  82. // JavaScript quit request callback.
  83. void DisplayServerJavaScript::request_quit_callback() {
  84. DisplayServerJavaScript *ds = get_singleton();
  85. if (ds && !ds->window_event_callback.is_null()) {
  86. Variant event = int(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST);
  87. Variant *eventp = &event;
  88. Variant ret;
  89. Callable::CallError ce;
  90. ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce);
  91. }
  92. }
  93. // Keys
  94. void DisplayServerJavaScript::dom2godot_mod(Ref<InputEventWithModifiers> ev, int p_mod) {
  95. ev->set_shift_pressed(p_mod & 1);
  96. ev->set_alt_pressed(p_mod & 2);
  97. ev->set_ctrl_pressed(p_mod & 4);
  98. ev->set_meta_pressed(p_mod & 8);
  99. }
  100. void DisplayServerJavaScript::key_callback(int p_pressed, int p_repeat, int p_modifiers) {
  101. DisplayServerJavaScript *ds = get_singleton();
  102. JSKeyEvent &key_event = ds->key_event;
  103. // Resume audio context after input in case autoplay was denied.
  104. OS_JavaScript::get_singleton()->resume_audio();
  105. Ref<InputEventKey> ev;
  106. ev.instantiate();
  107. ev->set_echo(p_repeat);
  108. ev->set_keycode(dom_code2godot_scancode(key_event.code, key_event.key, false));
  109. ev->set_physical_keycode(dom_code2godot_scancode(key_event.code, key_event.key, true));
  110. ev->set_pressed(p_pressed);
  111. dom2godot_mod(ev, p_modifiers);
  112. String unicode = String::utf8(key_event.key);
  113. if (unicode.length() == 1) {
  114. ev->set_unicode(unicode[0]);
  115. }
  116. Input::get_singleton()->parse_input_event(ev);
  117. // Make sure to flush all events so we can call restricted APIs inside the event.
  118. Input::get_singleton()->flush_buffered_events();
  119. }
  120. // Mouse
  121. int DisplayServerJavaScript::mouse_button_callback(int p_pressed, int p_button, double p_x, double p_y, int p_modifiers) {
  122. DisplayServerJavaScript *ds = get_singleton();
  123. Ref<InputEventMouseButton> ev;
  124. ev.instantiate();
  125. ev->set_pressed(p_pressed);
  126. ev->set_position(Point2(p_x, p_y));
  127. ev->set_global_position(ev->get_position());
  128. ev->set_pressed(p_pressed);
  129. dom2godot_mod(ev, p_modifiers);
  130. switch (p_button) {
  131. case DOM_BUTTON_LEFT:
  132. ev->set_button_index(MOUSE_BUTTON_LEFT);
  133. break;
  134. case DOM_BUTTON_MIDDLE:
  135. ev->set_button_index(MOUSE_BUTTON_MIDDLE);
  136. break;
  137. case DOM_BUTTON_RIGHT:
  138. ev->set_button_index(MOUSE_BUTTON_RIGHT);
  139. break;
  140. case DOM_BUTTON_XBUTTON1:
  141. ev->set_button_index(MOUSE_BUTTON_XBUTTON1);
  142. break;
  143. case DOM_BUTTON_XBUTTON2:
  144. ev->set_button_index(MOUSE_BUTTON_XBUTTON2);
  145. break;
  146. default:
  147. return false;
  148. }
  149. if (p_pressed) {
  150. uint64_t diff = (OS::get_singleton()->get_ticks_usec() / 1000) - ds->last_click_ms;
  151. if (ev->get_button_index() == ds->last_click_button_index) {
  152. if (diff < 400 && Point2(ds->last_click_pos).distance_to(ev->get_position()) < 5) {
  153. ds->last_click_ms = 0;
  154. ds->last_click_pos = Point2(-100, -100);
  155. ds->last_click_button_index = -1;
  156. ev->set_double_click(true);
  157. }
  158. } else {
  159. ds->last_click_button_index = ev->get_button_index();
  160. }
  161. if (!ev->is_double_click()) {
  162. ds->last_click_ms += diff;
  163. ds->last_click_pos = ev->get_position();
  164. }
  165. }
  166. int mask = Input::get_singleton()->get_mouse_button_mask();
  167. int button_flag = 1 << (ev->get_button_index() - 1);
  168. if (ev->is_pressed()) {
  169. mask |= button_flag;
  170. } else if (mask & button_flag) {
  171. mask &= ~button_flag;
  172. } else {
  173. // Received release event, but press was outside the canvas, so ignore.
  174. return false;
  175. }
  176. ev->set_button_mask(mask);
  177. Input::get_singleton()->parse_input_event(ev);
  178. // Resume audio context after input in case autoplay was denied.
  179. OS_JavaScript::get_singleton()->resume_audio();
  180. // Make sure to flush all events so we can call restricted APIs inside the event.
  181. Input::get_singleton()->flush_buffered_events();
  182. // Prevent multi-click text selection and wheel-click scrolling anchor.
  183. // Context menu is prevented through contextmenu event.
  184. return true;
  185. }
  186. void DisplayServerJavaScript::mouse_move_callback(double p_x, double p_y, double p_rel_x, double p_rel_y, int p_modifiers) {
  187. int input_mask = Input::get_singleton()->get_mouse_button_mask();
  188. // For motion outside the canvas, only read mouse movement if dragging
  189. // started inside the canvas; imitating desktop app behaviour.
  190. if (!get_singleton()->cursor_inside_canvas && !input_mask)
  191. return;
  192. Ref<InputEventMouseMotion> ev;
  193. ev.instantiate();
  194. dom2godot_mod(ev, p_modifiers);
  195. ev->set_button_mask(input_mask);
  196. ev->set_position(Point2(p_x, p_y));
  197. ev->set_global_position(ev->get_position());
  198. ev->set_relative(Vector2(p_rel_x, p_rel_y));
  199. Input::get_singleton()->set_mouse_position(ev->get_position());
  200. ev->set_speed(Input::get_singleton()->get_last_mouse_speed());
  201. Input::get_singleton()->parse_input_event(ev);
  202. }
  203. // Cursor
  204. const char *DisplayServerJavaScript::godot2dom_cursor(DisplayServer::CursorShape p_shape) {
  205. switch (p_shape) {
  206. case DisplayServer::CURSOR_ARROW:
  207. return "auto";
  208. case DisplayServer::CURSOR_IBEAM:
  209. return "text";
  210. case DisplayServer::CURSOR_POINTING_HAND:
  211. return "pointer";
  212. case DisplayServer::CURSOR_CROSS:
  213. return "crosshair";
  214. case DisplayServer::CURSOR_WAIT:
  215. return "progress";
  216. case DisplayServer::CURSOR_BUSY:
  217. return "wait";
  218. case DisplayServer::CURSOR_DRAG:
  219. return "grab";
  220. case DisplayServer::CURSOR_CAN_DROP:
  221. return "grabbing";
  222. case DisplayServer::CURSOR_FORBIDDEN:
  223. return "no-drop";
  224. case DisplayServer::CURSOR_VSIZE:
  225. return "ns-resize";
  226. case DisplayServer::CURSOR_HSIZE:
  227. return "ew-resize";
  228. case DisplayServer::CURSOR_BDIAGSIZE:
  229. return "nesw-resize";
  230. case DisplayServer::CURSOR_FDIAGSIZE:
  231. return "nwse-resize";
  232. case DisplayServer::CURSOR_MOVE:
  233. return "move";
  234. case DisplayServer::CURSOR_VSPLIT:
  235. return "row-resize";
  236. case DisplayServer::CURSOR_HSPLIT:
  237. return "col-resize";
  238. case DisplayServer::CURSOR_HELP:
  239. return "help";
  240. default:
  241. return "auto";
  242. }
  243. }
  244. void DisplayServerJavaScript::cursor_set_shape(CursorShape p_shape) {
  245. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  246. if (cursor_shape == p_shape) {
  247. return;
  248. }
  249. cursor_shape = p_shape;
  250. godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape));
  251. }
  252. DisplayServer::CursorShape DisplayServerJavaScript::cursor_get_shape() const {
  253. return cursor_shape;
  254. }
  255. void DisplayServerJavaScript::cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  256. if (p_cursor.is_valid()) {
  257. Ref<Texture2D> texture = p_cursor;
  258. Ref<AtlasTexture> atlas_texture = p_cursor;
  259. Ref<Image> image;
  260. Size2 texture_size;
  261. Rect2 atlas_rect;
  262. if (texture.is_valid()) {
  263. image = texture->get_image();
  264. }
  265. if (!image.is_valid() && atlas_texture.is_valid()) {
  266. texture = atlas_texture->get_atlas();
  267. atlas_rect.size.width = texture->get_width();
  268. atlas_rect.size.height = texture->get_height();
  269. atlas_rect.position.x = atlas_texture->get_region().position.x;
  270. atlas_rect.position.y = atlas_texture->get_region().position.y;
  271. texture_size.width = atlas_texture->get_region().size.x;
  272. texture_size.height = atlas_texture->get_region().size.y;
  273. } else if (image.is_valid()) {
  274. texture_size.width = texture->get_width();
  275. texture_size.height = texture->get_height();
  276. }
  277. ERR_FAIL_COND(!texture.is_valid());
  278. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  279. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  280. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  281. image = texture->get_image();
  282. ERR_FAIL_COND(!image.is_valid());
  283. image = image->duplicate();
  284. if (atlas_texture.is_valid())
  285. image->crop_from_point(
  286. atlas_rect.position.x,
  287. atlas_rect.position.y,
  288. texture_size.width,
  289. texture_size.height);
  290. if (image->get_format() != Image::FORMAT_RGBA8) {
  291. image->convert(Image::FORMAT_RGBA8);
  292. }
  293. png_image png_meta;
  294. memset(&png_meta, 0, sizeof png_meta);
  295. png_meta.version = PNG_IMAGE_VERSION;
  296. png_meta.width = texture_size.width;
  297. png_meta.height = texture_size.height;
  298. png_meta.format = PNG_FORMAT_RGBA;
  299. PackedByteArray png;
  300. size_t len;
  301. PackedByteArray data = image->get_data();
  302. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  303. png.resize(len);
  304. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  305. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), png.ptr(), len, p_hotspot.x, p_hotspot.y);
  306. } else {
  307. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), nullptr, 0, 0, 0);
  308. }
  309. cursor_set_shape(cursor_shape);
  310. }
  311. // Mouse mode
  312. void DisplayServerJavaScript::mouse_set_mode(MouseMode p_mode) {
  313. 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.");
  314. if (p_mode == mouse_get_mode()) {
  315. return;
  316. }
  317. if (p_mode == MOUSE_MODE_VISIBLE) {
  318. godot_js_display_cursor_set_visible(1);
  319. emscripten_exit_pointerlock();
  320. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  321. godot_js_display_cursor_set_visible(0);
  322. emscripten_exit_pointerlock();
  323. } else if (p_mode == MOUSE_MODE_CAPTURED) {
  324. godot_js_display_cursor_set_visible(1);
  325. EMSCRIPTEN_RESULT result = emscripten_request_pointerlock(canvas_id, false);
  326. ERR_FAIL_COND_MSG(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback.");
  327. ERR_FAIL_COND_MSG(result != EMSCRIPTEN_RESULT_SUCCESS, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback.");
  328. }
  329. }
  330. DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const {
  331. if (godot_js_display_cursor_is_hidden()) {
  332. return MOUSE_MODE_HIDDEN;
  333. }
  334. EmscriptenPointerlockChangeEvent ev;
  335. emscripten_get_pointerlock_status(&ev);
  336. return (ev.isActive && String::utf8(ev.id) == String::utf8(&canvas_id[1])) ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE;
  337. }
  338. // Wheel
  339. int DisplayServerJavaScript::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
  340. if (!godot_js_display_canvas_is_focused()) {
  341. if (get_singleton()->cursor_inside_canvas) {
  342. godot_js_display_canvas_focus();
  343. } else {
  344. return false;
  345. }
  346. }
  347. Input *input = Input::get_singleton();
  348. Ref<InputEventMouseButton> ev;
  349. ev.instantiate();
  350. ev->set_position(input->get_mouse_position());
  351. ev->set_global_position(ev->get_position());
  352. ev->set_shift_pressed(input->is_key_pressed(KEY_SHIFT));
  353. ev->set_alt_pressed(input->is_key_pressed(KEY_ALT));
  354. ev->set_ctrl_pressed(input->is_key_pressed(KEY_CTRL));
  355. ev->set_meta_pressed(input->is_key_pressed(KEY_META));
  356. if (p_delta_y < 0) {
  357. ev->set_button_index(MOUSE_BUTTON_WHEEL_UP);
  358. } else if (p_delta_y > 0) {
  359. ev->set_button_index(MOUSE_BUTTON_WHEEL_DOWN);
  360. } else if (p_delta_x > 0) {
  361. ev->set_button_index(MOUSE_BUTTON_WHEEL_LEFT);
  362. } else if (p_delta_x < 0) {
  363. ev->set_button_index(MOUSE_BUTTON_WHEEL_RIGHT);
  364. } else {
  365. return false;
  366. }
  367. // Different browsers give wildly different delta values, and we can't
  368. // interpret deltaMode, so use default value for wheel events' factor.
  369. int button_flag = 1 << (ev->get_button_index() - 1);
  370. ev->set_pressed(true);
  371. ev->set_button_mask(input->get_mouse_button_mask() | button_flag);
  372. input->parse_input_event(ev);
  373. Ref<InputEventMouseButton> release = ev->duplicate();
  374. release->set_pressed(false);
  375. release->set_button_mask(MouseButton(input->get_mouse_button_mask() & ~button_flag));
  376. input->parse_input_event(release);
  377. return true;
  378. }
  379. // Touch
  380. void DisplayServerJavaScript::touch_callback(int p_type, int p_count) {
  381. DisplayServerJavaScript *ds = get_singleton();
  382. const JSTouchEvent &touch_event = ds->touch_event;
  383. for (int i = 0; i < p_count; i++) {
  384. Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]);
  385. if (p_type == 2) {
  386. // touchmove
  387. Ref<InputEventScreenDrag> ev;
  388. ev.instantiate();
  389. ev->set_index(touch_event.identifier[i]);
  390. ev->set_position(point);
  391. Point2 &prev = ds->touches[i];
  392. ev->set_relative(ev->get_position() - prev);
  393. prev = ev->get_position();
  394. Input::get_singleton()->parse_input_event(ev);
  395. } else {
  396. // touchstart/touchend
  397. Ref<InputEventScreenTouch> ev;
  398. // Resume audio context after input in case autoplay was denied.
  399. OS_JavaScript::get_singleton()->resume_audio();
  400. ev.instantiate();
  401. ev->set_index(touch_event.identifier[i]);
  402. ev->set_position(point);
  403. ev->set_pressed(p_type == 0);
  404. ds->touches[i] = point;
  405. Input::get_singleton()->parse_input_event(ev);
  406. // Make sure to flush all events so we can call restricted APIs inside the event.
  407. Input::get_singleton()->flush_buffered_events();
  408. }
  409. }
  410. }
  411. bool DisplayServerJavaScript::screen_is_touchscreen(int p_screen) const {
  412. return godot_js_display_touchscreen_is_available();
  413. }
  414. // Virtual Keyboard
  415. void DisplayServerJavaScript::vk_input_text_callback(const char *p_text, int p_cursor) {
  416. DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton();
  417. if (!ds || ds->input_text_callback.is_null()) {
  418. return;
  419. }
  420. // Call input_text
  421. Variant event = String(p_text);
  422. Variant *eventp = &event;
  423. Variant ret;
  424. Callable::CallError ce;
  425. ds->input_text_callback.call((const Variant **)&eventp, 1, ret, ce);
  426. // Insert key right to reach position.
  427. Input *input = Input::get_singleton();
  428. Ref<InputEventKey> k;
  429. for (int i = 0; i < p_cursor; i++) {
  430. k.instantiate();
  431. k->set_pressed(true);
  432. k->set_echo(false);
  433. k->set_keycode(KEY_RIGHT);
  434. input->parse_input_event(k);
  435. k.instantiate();
  436. k->set_pressed(false);
  437. k->set_echo(false);
  438. k->set_keycode(KEY_RIGHT);
  439. input->parse_input_event(k);
  440. }
  441. }
  442. void DisplayServerJavaScript::virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect, bool p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
  443. godot_js_display_vk_show(p_existing_text.utf8().get_data(), p_multiline, p_cursor_start, p_cursor_end);
  444. }
  445. void DisplayServerJavaScript::virtual_keyboard_hide() {
  446. godot_js_display_vk_hide();
  447. }
  448. // Window blur callback
  449. EM_BOOL DisplayServerJavaScript::blur_callback(int p_event_type, const EmscriptenFocusEvent *p_event, void *p_user_data) {
  450. Input::get_singleton()->release_pressed_events();
  451. return false;
  452. }
  453. // Gamepad
  454. void DisplayServerJavaScript::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) {
  455. Input *input = Input::get_singleton();
  456. if (p_connected) {
  457. input->joy_connection_changed(p_index, true, String::utf8(p_id), String::utf8(p_guid));
  458. } else {
  459. input->joy_connection_changed(p_index, false, "");
  460. }
  461. }
  462. void DisplayServerJavaScript::process_joypads() {
  463. Input *input = Input::get_singleton();
  464. int32_t pads = godot_js_display_gamepad_sample_count();
  465. int32_t s_btns_num = 0;
  466. int32_t s_axes_num = 0;
  467. int32_t s_standard = 0;
  468. float s_btns[16];
  469. float s_axes[10];
  470. for (int idx = 0; idx < pads; idx++) {
  471. int err = godot_js_display_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
  472. if (err) {
  473. continue;
  474. }
  475. for (int b = 0; b < s_btns_num; b++) {
  476. float value = s_btns[b];
  477. // Buttons 6 and 7 in the standard mapping need to be
  478. // axis to be handled as JOY_AXIS_TRIGGER by Godot.
  479. if (s_standard && (b == 6 || b == 7)) {
  480. Input::JoyAxisValue joy_axis;
  481. joy_axis.min = 0;
  482. joy_axis.value = value;
  483. JoyAxis a = b == 6 ? JOY_AXIS_TRIGGER_LEFT : JOY_AXIS_TRIGGER_RIGHT;
  484. input->joy_axis(idx, a, joy_axis);
  485. } else {
  486. input->joy_button(idx, (JoyButton)b, value);
  487. }
  488. }
  489. for (int a = 0; a < s_axes_num; a++) {
  490. Input::JoyAxisValue joy_axis;
  491. joy_axis.min = -1;
  492. joy_axis.value = s_axes[a];
  493. input->joy_axis(idx, (JoyAxis)a, joy_axis);
  494. }
  495. }
  496. }
  497. Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() {
  498. Vector<String> drivers;
  499. drivers.push_back("dummy");
  500. return drivers;
  501. }
  502. // Clipboard
  503. void DisplayServerJavaScript::update_clipboard_callback(const char *p_text) {
  504. get_singleton()->clipboard = p_text;
  505. }
  506. void DisplayServerJavaScript::clipboard_set(const String &p_text) {
  507. clipboard = p_text;
  508. int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
  509. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  510. }
  511. String DisplayServerJavaScript::clipboard_get() const {
  512. godot_js_display_clipboard_get(update_clipboard_callback);
  513. return clipboard;
  514. }
  515. void DisplayServerJavaScript::send_window_event_callback(int p_notification) {
  516. DisplayServerJavaScript *ds = get_singleton();
  517. if (!ds) {
  518. return;
  519. }
  520. if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
  521. ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  522. }
  523. if (!ds->window_event_callback.is_null()) {
  524. Variant event = int(p_notification);
  525. Variant *eventp = &event;
  526. Variant ret;
  527. Callable::CallError ce;
  528. ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce);
  529. }
  530. }
  531. void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) {
  532. ERR_FAIL_COND(p_icon.is_null());
  533. Ref<Image> icon = p_icon;
  534. if (icon->is_compressed()) {
  535. icon = icon->duplicate();
  536. ERR_FAIL_COND(icon->decompress() != OK);
  537. }
  538. if (icon->get_format() != Image::FORMAT_RGBA8) {
  539. if (icon == p_icon)
  540. icon = icon->duplicate();
  541. icon->convert(Image::FORMAT_RGBA8);
  542. }
  543. png_image png_meta;
  544. memset(&png_meta, 0, sizeof png_meta);
  545. png_meta.version = PNG_IMAGE_VERSION;
  546. png_meta.width = icon->get_width();
  547. png_meta.height = icon->get_height();
  548. png_meta.format = PNG_FORMAT_RGBA;
  549. PackedByteArray png;
  550. size_t len;
  551. PackedByteArray data = icon->get_data();
  552. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  553. png.resize(len);
  554. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  555. godot_js_display_window_icon_set(png.ptr(), len);
  556. }
  557. void DisplayServerJavaScript::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  558. Callable cb = get_singleton()->input_event_callback;
  559. if (!cb.is_null()) {
  560. Variant ev = p_event;
  561. Variant *evp = &ev;
  562. Variant ret;
  563. Callable::CallError ce;
  564. cb.call((const Variant **)&evp, 1, ret, ce);
  565. }
  566. }
  567. 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) {
  568. return memnew(DisplayServerJavaScript(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  569. }
  570. 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) {
  571. r_error = OK; // Always succeeds for now.
  572. // Ensure the canvas ID.
  573. godot_js_config_canvas_id_get(canvas_id, 256);
  574. // Handle contextmenu, webglcontextlost
  575. godot_js_display_setup_canvas(p_resolution.x, p_resolution.y, p_window_mode == WINDOW_MODE_FULLSCREEN, OS::get_singleton()->is_hidpi_allowed() ? 1 : 0);
  576. // Check if it's windows.
  577. swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1;
  578. // Expose method for requesting quit.
  579. godot_js_os_request_quit_cb(request_quit_callback);
  580. RasterizerDummy::make_current(); // TODO GLES2 in Godot 4.0... or webgpu?
  581. #if 0
  582. EmscriptenWebGLContextAttributes attributes;
  583. emscripten_webgl_init_context_attributes(&attributes);
  584. attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
  585. attributes.antialias = false;
  586. ERR_FAIL_INDEX_V(p_video_driver, VIDEO_DRIVER_MAX, ERR_INVALID_PARAMETER);
  587. if (p_desired.layered) {
  588. set_window_per_pixel_transparency_enabled(true);
  589. }
  590. bool gl_initialization_error = false;
  591. if (RasterizerGLES2::is_viable() == OK) {
  592. attributes.majorVersion = 1;
  593. RasterizerGLES2::register_config();
  594. RasterizerGLES2::make_current();
  595. } else {
  596. gl_initialization_error = true;
  597. }
  598. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(canvas_id, &attributes);
  599. if (emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
  600. gl_initialization_error = true;
  601. }
  602. if (gl_initialization_error) {
  603. OS::get_singleton()->alert("Your browser does not seem to support WebGL. Please update your browser version.",
  604. "Unable to initialize video driver");
  605. return ERR_UNAVAILABLE;
  606. }
  607. video_driver_index = p_video_driver;
  608. #endif
  609. EMSCRIPTEN_RESULT result;
  610. #define EM_CHECK(ev) \
  611. if (result != EMSCRIPTEN_RESULT_SUCCESS) \
  612. ERR_PRINT("Error while setting " #ev " callback: Code " + itos(result));
  613. #define SET_EM_CALLBACK(target, ev, cb) \
  614. result = emscripten_set_##ev##_callback(target, nullptr, true, &cb); \
  615. EM_CHECK(ev)
  616. #define SET_EM_WINDOW_CALLBACK(ev, cb) \
  617. result = emscripten_set_##ev##_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, false, &cb); \
  618. EM_CHECK(ev)
  619. // These callbacks from Emscripten's html5.h suffice to access most
  620. // JavaScript APIs.
  621. SET_EM_WINDOW_CALLBACK(blur, blur_callback)
  622. #undef SET_EM_CALLBACK
  623. #undef EM_CHECK
  624. godot_js_display_mouse_button_cb(&DisplayServerJavaScript::mouse_button_callback);
  625. godot_js_display_mouse_move_cb(&DisplayServerJavaScript::mouse_move_callback);
  626. godot_js_display_mouse_wheel_cb(&DisplayServerJavaScript::mouse_wheel_callback);
  627. godot_js_display_touch_cb(&DisplayServerJavaScript::touch_callback, touch_event.identifier, touch_event.coords);
  628. godot_js_display_key_cb(&DisplayServerJavaScript::key_callback, key_event.code, key_event.key);
  629. godot_js_display_fullscreen_cb(&DisplayServerJavaScript::fullscreen_change_callback);
  630. godot_js_display_notification_cb(&send_window_event_callback,
  631. WINDOW_EVENT_MOUSE_ENTER,
  632. WINDOW_EVENT_MOUSE_EXIT,
  633. WINDOW_EVENT_FOCUS_IN,
  634. WINDOW_EVENT_FOCUS_OUT);
  635. godot_js_display_paste_cb(update_clipboard_callback);
  636. godot_js_display_drop_files_cb(drop_files_js_callback);
  637. godot_js_display_gamepad_cb(&DisplayServerJavaScript::gamepad_callback);
  638. godot_js_display_vk_cb(&vk_input_text_callback);
  639. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
  640. }
  641. DisplayServerJavaScript::~DisplayServerJavaScript() {
  642. //emscripten_webgl_commit_frame();
  643. //emscripten_webgl_destroy_context(webgl_ctx);
  644. }
  645. bool DisplayServerJavaScript::has_feature(Feature p_feature) const {
  646. switch (p_feature) {
  647. //case FEATURE_CONSOLE_WINDOW:
  648. //case FEATURE_GLOBAL_MENU:
  649. //case FEATURE_HIDPI:
  650. //case FEATURE_IME:
  651. case FEATURE_ICON:
  652. case FEATURE_CLIPBOARD:
  653. case FEATURE_CURSOR_SHAPE:
  654. case FEATURE_CUSTOM_CURSOR_SHAPE:
  655. case FEATURE_MOUSE:
  656. case FEATURE_TOUCHSCREEN:
  657. return true;
  658. //case FEATURE_MOUSE_WARP:
  659. //case FEATURE_NATIVE_DIALOG:
  660. //case FEATURE_NATIVE_ICON:
  661. //case FEATURE_WINDOW_TRANSPARENCY:
  662. //case FEATURE_KEEP_SCREEN_ON:
  663. //case FEATURE_ORIENTATION:
  664. case FEATURE_VIRTUAL_KEYBOARD:
  665. return godot_js_display_vk_available() != 0;
  666. default:
  667. return false;
  668. }
  669. }
  670. void DisplayServerJavaScript::register_javascript_driver() {
  671. register_create_function("javascript", create_func, get_rendering_drivers_func);
  672. }
  673. String DisplayServerJavaScript::get_name() const {
  674. return "javascript";
  675. }
  676. int DisplayServerJavaScript::get_screen_count() const {
  677. return 1;
  678. }
  679. Point2i DisplayServerJavaScript::screen_get_position(int p_screen) const {
  680. return Point2i(); // TODO offsetX/Y?
  681. }
  682. Size2i DisplayServerJavaScript::screen_get_size(int p_screen) const {
  683. int size[2];
  684. godot_js_display_screen_size_get(size, size + 1);
  685. return Size2(size[0], size[1]);
  686. }
  687. Rect2i DisplayServerJavaScript::screen_get_usable_rect(int p_screen) const {
  688. int size[2];
  689. godot_js_display_window_size_get(size, size + 1);
  690. return Rect2i(0, 0, size[0], size[1]);
  691. }
  692. int DisplayServerJavaScript::screen_get_dpi(int p_screen) const {
  693. return godot_js_display_screen_dpi_get();
  694. }
  695. float DisplayServerJavaScript::screen_get_scale(int p_screen) const {
  696. return godot_js_display_pixel_ratio_get();
  697. }
  698. Vector<DisplayServer::WindowID> DisplayServerJavaScript::get_window_list() const {
  699. Vector<WindowID> ret;
  700. ret.push_back(MAIN_WINDOW_ID);
  701. return ret;
  702. }
  703. DisplayServerJavaScript::WindowID DisplayServerJavaScript::get_window_at_screen_position(const Point2i &p_position) const {
  704. return MAIN_WINDOW_ID;
  705. }
  706. void DisplayServerJavaScript::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  707. window_attached_instance_id = p_instance;
  708. }
  709. ObjectID DisplayServerJavaScript::window_get_attached_instance_id(WindowID p_window) const {
  710. return window_attached_instance_id;
  711. }
  712. void DisplayServerJavaScript::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  713. // Not supported.
  714. }
  715. void DisplayServerJavaScript::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  716. window_event_callback = p_callable;
  717. }
  718. void DisplayServerJavaScript::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  719. input_event_callback = p_callable;
  720. }
  721. void DisplayServerJavaScript::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  722. input_text_callback = p_callable;
  723. }
  724. void DisplayServerJavaScript::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  725. drop_files_callback = p_callable;
  726. }
  727. void DisplayServerJavaScript::window_set_title(const String &p_title, WindowID p_window) {
  728. godot_js_display_window_title_set(p_title.utf8().get_data());
  729. }
  730. int DisplayServerJavaScript::window_get_current_screen(WindowID p_window) const {
  731. return 1;
  732. }
  733. void DisplayServerJavaScript::window_set_current_screen(int p_screen, WindowID p_window) {
  734. // Not implemented.
  735. }
  736. Point2i DisplayServerJavaScript::window_get_position(WindowID p_window) const {
  737. return Point2i(); // TODO Does this need implementation?
  738. }
  739. void DisplayServerJavaScript::window_set_position(const Point2i &p_position, WindowID p_window) {
  740. // Not supported.
  741. }
  742. void DisplayServerJavaScript::window_set_transient(WindowID p_window, WindowID p_parent) {
  743. // Not supported.
  744. }
  745. void DisplayServerJavaScript::window_set_max_size(const Size2i p_size, WindowID p_window) {
  746. // Not supported.
  747. }
  748. Size2i DisplayServerJavaScript::window_get_max_size(WindowID p_window) const {
  749. return Size2i();
  750. }
  751. void DisplayServerJavaScript::window_set_min_size(const Size2i p_size, WindowID p_window) {
  752. // Not supported.
  753. }
  754. Size2i DisplayServerJavaScript::window_get_min_size(WindowID p_window) const {
  755. return Size2i();
  756. }
  757. void DisplayServerJavaScript::window_set_size(const Size2i p_size, WindowID p_window) {
  758. godot_js_display_desired_size_set(p_size.x, p_size.y);
  759. }
  760. Size2i DisplayServerJavaScript::window_get_size(WindowID p_window) const {
  761. int size[2];
  762. godot_js_display_window_size_get(size, size + 1);
  763. return Size2i(size[0], size[1]);
  764. }
  765. Size2i DisplayServerJavaScript::window_get_real_size(WindowID p_window) const {
  766. return window_get_size(p_window);
  767. }
  768. void DisplayServerJavaScript::window_set_mode(WindowMode p_mode, WindowID p_window) {
  769. if (window_mode == p_mode)
  770. return;
  771. switch (p_mode) {
  772. case WINDOW_MODE_WINDOWED: {
  773. if (window_mode == WINDOW_MODE_FULLSCREEN) {
  774. godot_js_display_fullscreen_exit();
  775. }
  776. window_mode = WINDOW_MODE_WINDOWED;
  777. } break;
  778. case WINDOW_MODE_FULLSCREEN: {
  779. int result = godot_js_display_fullscreen_request();
  780. ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
  781. } break;
  782. case WINDOW_MODE_MAXIMIZED:
  783. case WINDOW_MODE_MINIMIZED:
  784. WARN_PRINT("WindowMode MAXIMIZED and MINIMIZED are not supported in HTML5 platform.");
  785. break;
  786. default:
  787. break;
  788. }
  789. }
  790. DisplayServerJavaScript::WindowMode DisplayServerJavaScript::window_get_mode(WindowID p_window) const {
  791. return window_mode;
  792. }
  793. bool DisplayServerJavaScript::window_is_maximize_allowed(WindowID p_window) const {
  794. return false;
  795. }
  796. void DisplayServerJavaScript::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  797. // Not supported.
  798. }
  799. bool DisplayServerJavaScript::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  800. return false;
  801. }
  802. void DisplayServerJavaScript::window_request_attention(WindowID p_window) {
  803. // Not supported.
  804. }
  805. void DisplayServerJavaScript::window_move_to_foreground(WindowID p_window) {
  806. // Not supported.
  807. }
  808. bool DisplayServerJavaScript::window_can_draw(WindowID p_window) const {
  809. return true;
  810. }
  811. bool DisplayServerJavaScript::can_any_window_draw() const {
  812. return true;
  813. }
  814. void DisplayServerJavaScript::process_events() {
  815. Input::get_singleton()->flush_buffered_events();
  816. if (godot_js_display_gamepad_sample() == OK) {
  817. process_joypads();
  818. }
  819. }
  820. int DisplayServerJavaScript::get_current_video_driver() const {
  821. return 1;
  822. }
  823. bool DisplayServerJavaScript::get_swap_cancel_ok() {
  824. return swap_cancel_ok;
  825. }
  826. void DisplayServerJavaScript::swap_buffers() {
  827. //emscripten_webgl_commit_frame();
  828. }