display_server_javascript.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 "platform/javascript/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/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.call((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.call((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 "auto";
  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 "progress";
  214. case DisplayServer::CURSOR_BUSY:
  215. return "wait";
  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 "auto";
  240. }
  241. }
  242. void DisplayServerJavaScript::cursor_set_shape(CursorShape p_shape) {
  243. ERR_FAIL_INDEX(p_shape, CURSOR_MAX);
  244. if (cursor_shape == p_shape) {
  245. return;
  246. }
  247. cursor_shape = p_shape;
  248. godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape));
  249. }
  250. DisplayServer::CursorShape DisplayServerJavaScript::cursor_get_shape() const {
  251. return cursor_shape;
  252. }
  253. void DisplayServerJavaScript::cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  254. if (p_cursor.is_valid()) {
  255. Ref<Texture2D> texture = p_cursor;
  256. Ref<AtlasTexture> atlas_texture = p_cursor;
  257. Ref<Image> image;
  258. Size2 texture_size;
  259. Rect2 atlas_rect;
  260. if (texture.is_valid()) {
  261. image = texture->get_image();
  262. }
  263. if (!image.is_valid() && atlas_texture.is_valid()) {
  264. texture = atlas_texture->get_atlas();
  265. atlas_rect.size.width = texture->get_width();
  266. atlas_rect.size.height = texture->get_height();
  267. atlas_rect.position.x = atlas_texture->get_region().position.x;
  268. atlas_rect.position.y = atlas_texture->get_region().position.y;
  269. texture_size.width = atlas_texture->get_region().size.x;
  270. texture_size.height = atlas_texture->get_region().size.y;
  271. } else if (image.is_valid()) {
  272. texture_size.width = texture->get_width();
  273. texture_size.height = texture->get_height();
  274. }
  275. ERR_FAIL_COND(!texture.is_valid());
  276. ERR_FAIL_COND(p_hotspot.x < 0 || p_hotspot.y < 0);
  277. ERR_FAIL_COND(texture_size.width > 256 || texture_size.height > 256);
  278. ERR_FAIL_COND(p_hotspot.x > texture_size.width || p_hotspot.y > texture_size.height);
  279. image = texture->get_image();
  280. ERR_FAIL_COND(!image.is_valid());
  281. image = image->duplicate();
  282. if (atlas_texture.is_valid())
  283. image->crop_from_point(
  284. atlas_rect.position.x,
  285. atlas_rect.position.y,
  286. texture_size.width,
  287. texture_size.height);
  288. if (image->get_format() != Image::FORMAT_RGBA8) {
  289. image->convert(Image::FORMAT_RGBA8);
  290. }
  291. png_image png_meta;
  292. memset(&png_meta, 0, sizeof png_meta);
  293. png_meta.version = PNG_IMAGE_VERSION;
  294. png_meta.width = texture_size.width;
  295. png_meta.height = texture_size.height;
  296. png_meta.format = PNG_FORMAT_RGBA;
  297. PackedByteArray png;
  298. size_t len;
  299. PackedByteArray data = image->get_data();
  300. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  301. png.resize(len);
  302. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  303. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), png.ptr(), len, p_hotspot.x, p_hotspot.y);
  304. } else {
  305. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), nullptr, 0, 0, 0);
  306. }
  307. cursor_set_shape(cursor_shape);
  308. }
  309. // Mouse mode
  310. void DisplayServerJavaScript::mouse_set_mode(MouseMode p_mode) {
  311. 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.");
  312. if (p_mode == mouse_get_mode()) {
  313. return;
  314. }
  315. if (p_mode == MOUSE_MODE_VISIBLE) {
  316. godot_js_display_cursor_set_visible(1);
  317. godot_js_display_cursor_lock_set(0);
  318. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  319. godot_js_display_cursor_set_visible(0);
  320. godot_js_display_cursor_lock_set(0);
  321. } else if (p_mode == MOUSE_MODE_CAPTURED) {
  322. godot_js_display_cursor_set_visible(1);
  323. godot_js_display_cursor_lock_set(1);
  324. }
  325. }
  326. DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const {
  327. if (godot_js_display_cursor_is_hidden()) {
  328. return MOUSE_MODE_HIDDEN;
  329. }
  330. if (godot_js_display_cursor_is_locked()) {
  331. return MOUSE_MODE_CAPTURED;
  332. }
  333. return MOUSE_MODE_VISIBLE;
  334. }
  335. Point2i DisplayServerJavaScript::mouse_get_position() const {
  336. return Input::get_singleton()->get_mouse_position();
  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(MouseButton::WHEEL_UP);
  358. } else if (p_delta_y > 0) {
  359. ev->set_button_index(MouseButton::WHEEL_DOWN);
  360. } else if (p_delta_x > 0) {
  361. ev->set_button_index(MouseButton::WHEEL_LEFT);
  362. } else if (p_delta_x < 0) {
  363. ev->set_button_index(MouseButton::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. MouseButton button_flag = mouse_button_to_mask(ev->get_button_index());
  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::utf8(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. void DisplayServerJavaScript::window_blur_callback() {
  449. Input::get_singleton()->release_pressed_events();
  450. }
  451. // Gamepad
  452. void DisplayServerJavaScript::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) {
  453. Input *input = Input::get_singleton();
  454. if (p_connected) {
  455. input->joy_connection_changed(p_index, true, String::utf8(p_id), String::utf8(p_guid));
  456. } else {
  457. input->joy_connection_changed(p_index, false, "");
  458. }
  459. }
  460. void DisplayServerJavaScript::process_joypads() {
  461. Input *input = Input::get_singleton();
  462. int32_t pads = godot_js_input_gamepad_sample_count();
  463. int32_t s_btns_num = 0;
  464. int32_t s_axes_num = 0;
  465. int32_t s_standard = 0;
  466. float s_btns[16];
  467. float s_axes[10];
  468. for (int idx = 0; idx < pads; idx++) {
  469. int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
  470. if (err) {
  471. continue;
  472. }
  473. for (int b = 0; b < s_btns_num; b++) {
  474. // Buttons 6 and 7 in the standard mapping need to be
  475. // axis to be handled as JoyAxis::TRIGGER by Godot.
  476. if (s_standard && (b == 6 || b == 7)) {
  477. input->joy_axis(idx, (JoyAxis)b, s_btns[b]);
  478. } else {
  479. input->joy_button(idx, (JoyButton)b, s_btns[b]);
  480. }
  481. }
  482. for (int a = 0; a < s_axes_num; a++) {
  483. input->joy_axis(idx, (JoyAxis)a, s_axes[a]);
  484. }
  485. }
  486. }
  487. Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() {
  488. Vector<String> drivers;
  489. #ifdef GLES3_ENABLED
  490. drivers.push_back("opengl3");
  491. #endif
  492. return drivers;
  493. }
  494. // Clipboard
  495. void DisplayServerJavaScript::update_clipboard_callback(const char *p_text) {
  496. get_singleton()->clipboard = String::utf8(p_text);
  497. }
  498. void DisplayServerJavaScript::clipboard_set(const String &p_text) {
  499. clipboard = p_text;
  500. int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
  501. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  502. }
  503. String DisplayServerJavaScript::clipboard_get() const {
  504. godot_js_display_clipboard_get(update_clipboard_callback);
  505. return clipboard;
  506. }
  507. void DisplayServerJavaScript::send_window_event_callback(int p_notification) {
  508. DisplayServerJavaScript *ds = get_singleton();
  509. if (!ds) {
  510. return;
  511. }
  512. if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
  513. ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  514. }
  515. if (!ds->window_event_callback.is_null()) {
  516. Variant event = int(p_notification);
  517. Variant *eventp = &event;
  518. Variant ret;
  519. Callable::CallError ce;
  520. ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce);
  521. }
  522. }
  523. void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) {
  524. ERR_FAIL_COND(p_icon.is_null());
  525. Ref<Image> icon = p_icon;
  526. if (icon->is_compressed()) {
  527. icon = icon->duplicate();
  528. ERR_FAIL_COND(icon->decompress() != OK);
  529. }
  530. if (icon->get_format() != Image::FORMAT_RGBA8) {
  531. if (icon == p_icon)
  532. icon = icon->duplicate();
  533. icon->convert(Image::FORMAT_RGBA8);
  534. }
  535. png_image png_meta;
  536. memset(&png_meta, 0, sizeof png_meta);
  537. png_meta.version = PNG_IMAGE_VERSION;
  538. png_meta.width = icon->get_width();
  539. png_meta.height = icon->get_height();
  540. png_meta.format = PNG_FORMAT_RGBA;
  541. PackedByteArray png;
  542. size_t len;
  543. PackedByteArray data = icon->get_data();
  544. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  545. png.resize(len);
  546. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  547. godot_js_display_window_icon_set(png.ptr(), len);
  548. }
  549. void DisplayServerJavaScript::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  550. Callable cb = get_singleton()->input_event_callback;
  551. if (!cb.is_null()) {
  552. Variant ev = p_event;
  553. Variant *evp = &ev;
  554. Variant ret;
  555. Callable::CallError ce;
  556. cb.call((const Variant **)&evp, 1, ret, ce);
  557. }
  558. }
  559. 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) {
  560. return memnew(DisplayServerJavaScript(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  561. }
  562. 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) {
  563. r_error = OK; // Always succeeds for now.
  564. // Ensure the canvas ID.
  565. godot_js_config_canvas_id_get(canvas_id, 256);
  566. // Handle contextmenu, webglcontextlost
  567. 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);
  568. // Check if it's windows.
  569. swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1;
  570. // Expose method for requesting quit.
  571. godot_js_os_request_quit_cb(request_quit_callback);
  572. #ifdef GLES3_ENABLED
  573. // TODO "vulkan" defaults to webgl2 for now.
  574. bool wants_webgl2 = p_rendering_driver == "opengl3" || p_rendering_driver == "vulkan";
  575. bool webgl2_init_failed = wants_webgl2 && !godot_js_display_has_webgl(2);
  576. if (wants_webgl2 && !webgl2_init_failed) {
  577. EmscriptenWebGLContextAttributes attributes;
  578. emscripten_webgl_init_context_attributes(&attributes);
  579. //attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
  580. attributes.alpha = true;
  581. attributes.antialias = false;
  582. attributes.majorVersion = 2;
  583. webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes);
  584. if (emscripten_webgl_make_context_current(webgl_ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
  585. webgl2_init_failed = true;
  586. } else {
  587. RasterizerGLES3::make_current();
  588. }
  589. }
  590. if (webgl2_init_failed) {
  591. OS::get_singleton()->alert("Your browser does not seem to support WebGL2. Please update your browser version.",
  592. "Unable to initialize video driver");
  593. }
  594. if (!wants_webgl2 || webgl2_init_failed) {
  595. RasterizerDummy::make_current();
  596. }
  597. #else
  598. RasterizerDummy::make_current();
  599. #endif
  600. // JS Input interface (js/libs/library_godot_input.js)
  601. godot_js_input_mouse_button_cb(&DisplayServerJavaScript::mouse_button_callback);
  602. godot_js_input_mouse_move_cb(&DisplayServerJavaScript::mouse_move_callback);
  603. godot_js_input_mouse_wheel_cb(&DisplayServerJavaScript::mouse_wheel_callback);
  604. godot_js_input_touch_cb(&DisplayServerJavaScript::touch_callback, touch_event.identifier, touch_event.coords);
  605. godot_js_input_key_cb(&DisplayServerJavaScript::key_callback, key_event.code, key_event.key);
  606. godot_js_input_paste_cb(update_clipboard_callback);
  607. godot_js_input_drop_files_cb(drop_files_js_callback);
  608. godot_js_input_gamepad_cb(&DisplayServerJavaScript::gamepad_callback);
  609. // JS Display interface (js/libs/library_godot_display.js)
  610. godot_js_display_fullscreen_cb(&DisplayServerJavaScript::fullscreen_change_callback);
  611. godot_js_display_window_blur_cb(&window_blur_callback);
  612. godot_js_display_notification_cb(&send_window_event_callback,
  613. WINDOW_EVENT_MOUSE_ENTER,
  614. WINDOW_EVENT_MOUSE_EXIT,
  615. WINDOW_EVENT_FOCUS_IN,
  616. WINDOW_EVENT_FOCUS_OUT);
  617. godot_js_display_vk_cb(&vk_input_text_callback);
  618. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
  619. }
  620. DisplayServerJavaScript::~DisplayServerJavaScript() {
  621. #ifdef GLES3_ENABLED
  622. if (webgl_ctx) {
  623. emscripten_webgl_commit_frame();
  624. emscripten_webgl_destroy_context(webgl_ctx);
  625. }
  626. #endif
  627. }
  628. bool DisplayServerJavaScript::has_feature(Feature p_feature) const {
  629. switch (p_feature) {
  630. //case FEATURE_GLOBAL_MENU:
  631. //case FEATURE_HIDPI:
  632. //case FEATURE_IME:
  633. case FEATURE_ICON:
  634. case FEATURE_CLIPBOARD:
  635. case FEATURE_CURSOR_SHAPE:
  636. case FEATURE_CUSTOM_CURSOR_SHAPE:
  637. case FEATURE_MOUSE:
  638. case FEATURE_TOUCHSCREEN:
  639. return true;
  640. //case FEATURE_MOUSE_WARP:
  641. //case FEATURE_NATIVE_DIALOG:
  642. //case FEATURE_NATIVE_ICON:
  643. //case FEATURE_WINDOW_TRANSPARENCY:
  644. //case FEATURE_KEEP_SCREEN_ON:
  645. //case FEATURE_ORIENTATION:
  646. case FEATURE_VIRTUAL_KEYBOARD:
  647. return godot_js_display_vk_available() != 0;
  648. default:
  649. return false;
  650. }
  651. }
  652. void DisplayServerJavaScript::register_javascript_driver() {
  653. register_create_function("javascript", create_func, get_rendering_drivers_func);
  654. }
  655. String DisplayServerJavaScript::get_name() const {
  656. return "javascript";
  657. }
  658. int DisplayServerJavaScript::get_screen_count() const {
  659. return 1;
  660. }
  661. Point2i DisplayServerJavaScript::screen_get_position(int p_screen) const {
  662. return Point2i(); // TODO offsetX/Y?
  663. }
  664. Size2i DisplayServerJavaScript::screen_get_size(int p_screen) const {
  665. int size[2];
  666. godot_js_display_screen_size_get(size, size + 1);
  667. return Size2(size[0], size[1]);
  668. }
  669. Rect2i DisplayServerJavaScript::screen_get_usable_rect(int p_screen) const {
  670. int size[2];
  671. godot_js_display_window_size_get(size, size + 1);
  672. return Rect2i(0, 0, size[0], size[1]);
  673. }
  674. int DisplayServerJavaScript::screen_get_dpi(int p_screen) const {
  675. return godot_js_display_screen_dpi_get();
  676. }
  677. float DisplayServerJavaScript::screen_get_scale(int p_screen) const {
  678. return godot_js_display_pixel_ratio_get();
  679. }
  680. float DisplayServerJavaScript::screen_get_refresh_rate(int p_screen) const {
  681. 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.
  682. }
  683. Vector<DisplayServer::WindowID> DisplayServerJavaScript::get_window_list() const {
  684. Vector<WindowID> ret;
  685. ret.push_back(MAIN_WINDOW_ID);
  686. return ret;
  687. }
  688. DisplayServerJavaScript::WindowID DisplayServerJavaScript::get_window_at_screen_position(const Point2i &p_position) const {
  689. return MAIN_WINDOW_ID;
  690. }
  691. void DisplayServerJavaScript::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  692. window_attached_instance_id = p_instance;
  693. }
  694. ObjectID DisplayServerJavaScript::window_get_attached_instance_id(WindowID p_window) const {
  695. return window_attached_instance_id;
  696. }
  697. void DisplayServerJavaScript::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  698. // Not supported.
  699. }
  700. void DisplayServerJavaScript::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  701. window_event_callback = p_callable;
  702. }
  703. void DisplayServerJavaScript::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  704. input_event_callback = p_callable;
  705. }
  706. void DisplayServerJavaScript::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  707. input_text_callback = p_callable;
  708. }
  709. void DisplayServerJavaScript::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  710. drop_files_callback = p_callable;
  711. }
  712. void DisplayServerJavaScript::window_set_title(const String &p_title, WindowID p_window) {
  713. godot_js_display_window_title_set(p_title.utf8().get_data());
  714. }
  715. int DisplayServerJavaScript::window_get_current_screen(WindowID p_window) const {
  716. return 1;
  717. }
  718. void DisplayServerJavaScript::window_set_current_screen(int p_screen, WindowID p_window) {
  719. // Not implemented.
  720. }
  721. Point2i DisplayServerJavaScript::window_get_position(WindowID p_window) const {
  722. return Point2i(); // TODO Does this need implementation?
  723. }
  724. void DisplayServerJavaScript::window_set_position(const Point2i &p_position, WindowID p_window) {
  725. // Not supported.
  726. }
  727. void DisplayServerJavaScript::window_set_transient(WindowID p_window, WindowID p_parent) {
  728. // Not supported.
  729. }
  730. void DisplayServerJavaScript::window_set_max_size(const Size2i p_size, WindowID p_window) {
  731. // Not supported.
  732. }
  733. Size2i DisplayServerJavaScript::window_get_max_size(WindowID p_window) const {
  734. return Size2i();
  735. }
  736. void DisplayServerJavaScript::window_set_min_size(const Size2i p_size, WindowID p_window) {
  737. // Not supported.
  738. }
  739. Size2i DisplayServerJavaScript::window_get_min_size(WindowID p_window) const {
  740. return Size2i();
  741. }
  742. void DisplayServerJavaScript::window_set_size(const Size2i p_size, WindowID p_window) {
  743. godot_js_display_desired_size_set(p_size.x, p_size.y);
  744. }
  745. Size2i DisplayServerJavaScript::window_get_size(WindowID p_window) const {
  746. int size[2];
  747. godot_js_display_window_size_get(size, size + 1);
  748. return Size2i(size[0], size[1]);
  749. }
  750. Size2i DisplayServerJavaScript::window_get_real_size(WindowID p_window) const {
  751. return window_get_size(p_window);
  752. }
  753. void DisplayServerJavaScript::window_set_mode(WindowMode p_mode, WindowID p_window) {
  754. if (window_mode == p_mode)
  755. return;
  756. switch (p_mode) {
  757. case WINDOW_MODE_WINDOWED: {
  758. if (window_mode == WINDOW_MODE_FULLSCREEN) {
  759. godot_js_display_fullscreen_exit();
  760. }
  761. window_mode = WINDOW_MODE_WINDOWED;
  762. } break;
  763. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  764. case WINDOW_MODE_FULLSCREEN: {
  765. int result = godot_js_display_fullscreen_request();
  766. ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
  767. } break;
  768. case WINDOW_MODE_MAXIMIZED:
  769. case WINDOW_MODE_MINIMIZED:
  770. WARN_PRINT("WindowMode MAXIMIZED and MINIMIZED are not supported in HTML5 platform.");
  771. break;
  772. default:
  773. break;
  774. }
  775. }
  776. DisplayServerJavaScript::WindowMode DisplayServerJavaScript::window_get_mode(WindowID p_window) const {
  777. return window_mode;
  778. }
  779. bool DisplayServerJavaScript::window_is_maximize_allowed(WindowID p_window) const {
  780. return false;
  781. }
  782. void DisplayServerJavaScript::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  783. // Not supported.
  784. }
  785. bool DisplayServerJavaScript::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  786. return false;
  787. }
  788. void DisplayServerJavaScript::window_request_attention(WindowID p_window) {
  789. // Not supported.
  790. }
  791. void DisplayServerJavaScript::window_move_to_foreground(WindowID p_window) {
  792. // Not supported.
  793. }
  794. bool DisplayServerJavaScript::window_can_draw(WindowID p_window) const {
  795. return true;
  796. }
  797. bool DisplayServerJavaScript::can_any_window_draw() const {
  798. return true;
  799. }
  800. void DisplayServerJavaScript::process_events() {
  801. Input::get_singleton()->flush_buffered_events();
  802. if (godot_js_input_gamepad_sample() == OK) {
  803. process_joypads();
  804. }
  805. }
  806. int DisplayServerJavaScript::get_current_video_driver() const {
  807. return 1;
  808. }
  809. bool DisplayServerJavaScript::get_swap_cancel_ok() {
  810. return swap_cancel_ok;
  811. }
  812. void DisplayServerJavaScript::swap_buffers() {
  813. #ifdef GLES3_ENABLED
  814. if (webgl_ctx) {
  815. emscripten_webgl_commit_frame();
  816. }
  817. #endif
  818. }