display_server_javascript.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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/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. }
  289. if (image->get_format() != Image::FORMAT_RGBA8) {
  290. image->convert(Image::FORMAT_RGBA8);
  291. }
  292. png_image png_meta;
  293. memset(&png_meta, 0, sizeof png_meta);
  294. png_meta.version = PNG_IMAGE_VERSION;
  295. png_meta.width = texture_size.width;
  296. png_meta.height = texture_size.height;
  297. png_meta.format = PNG_FORMAT_RGBA;
  298. PackedByteArray png;
  299. size_t len;
  300. PackedByteArray data = image->get_data();
  301. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  302. png.resize(len);
  303. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  304. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), png.ptr(), len, p_hotspot.x, p_hotspot.y);
  305. } else {
  306. godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), nullptr, 0, 0, 0);
  307. }
  308. cursor_set_shape(cursor_shape);
  309. }
  310. // Mouse mode
  311. void DisplayServerJavaScript::mouse_set_mode(MouseMode p_mode) {
  312. 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.");
  313. if (p_mode == mouse_get_mode()) {
  314. return;
  315. }
  316. if (p_mode == MOUSE_MODE_VISIBLE) {
  317. godot_js_display_cursor_set_visible(1);
  318. godot_js_display_cursor_lock_set(0);
  319. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  320. godot_js_display_cursor_set_visible(0);
  321. godot_js_display_cursor_lock_set(0);
  322. } else if (p_mode == MOUSE_MODE_CAPTURED) {
  323. godot_js_display_cursor_set_visible(1);
  324. godot_js_display_cursor_lock_set(1);
  325. }
  326. }
  327. DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const {
  328. if (godot_js_display_cursor_is_hidden()) {
  329. return MOUSE_MODE_HIDDEN;
  330. }
  331. if (godot_js_display_cursor_is_locked()) {
  332. return MOUSE_MODE_CAPTURED;
  333. }
  334. return MOUSE_MODE_VISIBLE;
  335. }
  336. Point2i DisplayServerJavaScript::mouse_get_position() const {
  337. return Input::get_singleton()->get_mouse_position();
  338. }
  339. // Wheel
  340. int DisplayServerJavaScript::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
  341. if (!godot_js_display_canvas_is_focused()) {
  342. if (get_singleton()->cursor_inside_canvas) {
  343. godot_js_display_canvas_focus();
  344. } else {
  345. return false;
  346. }
  347. }
  348. Input *input = Input::get_singleton();
  349. Ref<InputEventMouseButton> ev;
  350. ev.instantiate();
  351. ev->set_position(input->get_mouse_position());
  352. ev->set_global_position(ev->get_position());
  353. ev->set_shift_pressed(input->is_key_pressed(Key::SHIFT));
  354. ev->set_alt_pressed(input->is_key_pressed(Key::ALT));
  355. ev->set_ctrl_pressed(input->is_key_pressed(Key::CTRL));
  356. ev->set_meta_pressed(input->is_key_pressed(Key::META));
  357. if (p_delta_y < 0) {
  358. ev->set_button_index(MouseButton::WHEEL_UP);
  359. } else if (p_delta_y > 0) {
  360. ev->set_button_index(MouseButton::WHEEL_DOWN);
  361. } else if (p_delta_x > 0) {
  362. ev->set_button_index(MouseButton::WHEEL_LEFT);
  363. } else if (p_delta_x < 0) {
  364. ev->set_button_index(MouseButton::WHEEL_RIGHT);
  365. } else {
  366. return false;
  367. }
  368. // Different browsers give wildly different delta values, and we can't
  369. // interpret deltaMode, so use default value for wheel events' factor.
  370. MouseButton button_flag = mouse_button_to_mask(ev->get_button_index());
  371. ev->set_pressed(true);
  372. ev->set_button_mask(input->get_mouse_button_mask() | button_flag);
  373. input->parse_input_event(ev);
  374. Ref<InputEventMouseButton> release = ev->duplicate();
  375. release->set_pressed(false);
  376. release->set_button_mask(MouseButton(input->get_mouse_button_mask() & ~button_flag));
  377. input->parse_input_event(release);
  378. return true;
  379. }
  380. // Touch
  381. void DisplayServerJavaScript::touch_callback(int p_type, int p_count) {
  382. DisplayServerJavaScript *ds = get_singleton();
  383. const JSTouchEvent &touch_event = ds->touch_event;
  384. for (int i = 0; i < p_count; i++) {
  385. Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]);
  386. if (p_type == 2) {
  387. // touchmove
  388. Ref<InputEventScreenDrag> ev;
  389. ev.instantiate();
  390. ev->set_index(touch_event.identifier[i]);
  391. ev->set_position(point);
  392. Point2 &prev = ds->touches[i];
  393. ev->set_relative(ev->get_position() - prev);
  394. prev = ev->get_position();
  395. Input::get_singleton()->parse_input_event(ev);
  396. } else {
  397. // touchstart/touchend
  398. Ref<InputEventScreenTouch> ev;
  399. // Resume audio context after input in case autoplay was denied.
  400. OS_JavaScript::get_singleton()->resume_audio();
  401. ev.instantiate();
  402. ev->set_index(touch_event.identifier[i]);
  403. ev->set_position(point);
  404. ev->set_pressed(p_type == 0);
  405. ds->touches[i] = point;
  406. Input::get_singleton()->parse_input_event(ev);
  407. // Make sure to flush all events so we can call restricted APIs inside the event.
  408. Input::get_singleton()->flush_buffered_events();
  409. }
  410. }
  411. }
  412. bool DisplayServerJavaScript::screen_is_touchscreen(int p_screen) const {
  413. return godot_js_display_touchscreen_is_available();
  414. }
  415. // Virtual Keyboard
  416. void DisplayServerJavaScript::vk_input_text_callback(const char *p_text, int p_cursor) {
  417. DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton();
  418. if (!ds || ds->input_text_callback.is_null()) {
  419. return;
  420. }
  421. // Call input_text
  422. Variant event = String::utf8(p_text);
  423. Variant *eventp = &event;
  424. Variant ret;
  425. Callable::CallError ce;
  426. ds->input_text_callback.call((const Variant **)&eventp, 1, ret, ce);
  427. // Insert key right to reach position.
  428. Input *input = Input::get_singleton();
  429. Ref<InputEventKey> k;
  430. for (int i = 0; i < p_cursor; i++) {
  431. k.instantiate();
  432. k->set_pressed(true);
  433. k->set_echo(false);
  434. k->set_keycode(Key::RIGHT);
  435. input->parse_input_event(k);
  436. k.instantiate();
  437. k->set_pressed(false);
  438. k->set_echo(false);
  439. k->set_keycode(Key::RIGHT);
  440. input->parse_input_event(k);
  441. }
  442. }
  443. 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) {
  444. godot_js_display_vk_show(p_existing_text.utf8().get_data(), p_multiline, p_cursor_start, p_cursor_end);
  445. }
  446. void DisplayServerJavaScript::virtual_keyboard_hide() {
  447. godot_js_display_vk_hide();
  448. }
  449. void DisplayServerJavaScript::window_blur_callback() {
  450. Input::get_singleton()->release_pressed_events();
  451. }
  452. // Gamepad
  453. void DisplayServerJavaScript::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) {
  454. Input *input = Input::get_singleton();
  455. if (p_connected) {
  456. input->joy_connection_changed(p_index, true, String::utf8(p_id), String::utf8(p_guid));
  457. } else {
  458. input->joy_connection_changed(p_index, false, "");
  459. }
  460. }
  461. void DisplayServerJavaScript::process_joypads() {
  462. Input *input = Input::get_singleton();
  463. int32_t pads = godot_js_input_gamepad_sample_count();
  464. int32_t s_btns_num = 0;
  465. int32_t s_axes_num = 0;
  466. int32_t s_standard = 0;
  467. float s_btns[16];
  468. float s_axes[10];
  469. for (int idx = 0; idx < pads; idx++) {
  470. int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
  471. if (err) {
  472. continue;
  473. }
  474. for (int b = 0; b < s_btns_num; b++) {
  475. // Buttons 6 and 7 in the standard mapping need to be
  476. // axis to be handled as JoyAxis::TRIGGER by Godot.
  477. if (s_standard && (b == 6 || b == 7)) {
  478. input->joy_axis(idx, (JoyAxis)b, s_btns[b]);
  479. } else {
  480. input->joy_button(idx, (JoyButton)b, s_btns[b]);
  481. }
  482. }
  483. for (int a = 0; a < s_axes_num; a++) {
  484. input->joy_axis(idx, (JoyAxis)a, s_axes[a]);
  485. }
  486. }
  487. }
  488. Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() {
  489. Vector<String> drivers;
  490. #ifdef GLES3_ENABLED
  491. drivers.push_back("opengl3");
  492. #endif
  493. return drivers;
  494. }
  495. // Clipboard
  496. void DisplayServerJavaScript::update_clipboard_callback(const char *p_text) {
  497. get_singleton()->clipboard = String::utf8(p_text);
  498. }
  499. void DisplayServerJavaScript::clipboard_set(const String &p_text) {
  500. clipboard = p_text;
  501. int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
  502. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  503. }
  504. String DisplayServerJavaScript::clipboard_get() const {
  505. godot_js_display_clipboard_get(update_clipboard_callback);
  506. return clipboard;
  507. }
  508. void DisplayServerJavaScript::send_window_event_callback(int p_notification) {
  509. DisplayServerJavaScript *ds = get_singleton();
  510. if (!ds) {
  511. return;
  512. }
  513. if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
  514. ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  515. }
  516. if (!ds->window_event_callback.is_null()) {
  517. Variant event = int(p_notification);
  518. Variant *eventp = &event;
  519. Variant ret;
  520. Callable::CallError ce;
  521. ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce);
  522. }
  523. }
  524. void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) {
  525. ERR_FAIL_COND(p_icon.is_null());
  526. Ref<Image> icon = p_icon;
  527. if (icon->is_compressed()) {
  528. icon = icon->duplicate();
  529. ERR_FAIL_COND(icon->decompress() != OK);
  530. }
  531. if (icon->get_format() != Image::FORMAT_RGBA8) {
  532. if (icon == p_icon) {
  533. icon = icon->duplicate();
  534. }
  535. icon->convert(Image::FORMAT_RGBA8);
  536. }
  537. png_image png_meta;
  538. memset(&png_meta, 0, sizeof png_meta);
  539. png_meta.version = PNG_IMAGE_VERSION;
  540. png_meta.width = icon->get_width();
  541. png_meta.height = icon->get_height();
  542. png_meta.format = PNG_FORMAT_RGBA;
  543. PackedByteArray png;
  544. size_t len;
  545. PackedByteArray data = icon->get_data();
  546. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  547. png.resize(len);
  548. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  549. godot_js_display_window_icon_set(png.ptr(), len);
  550. }
  551. void DisplayServerJavaScript::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  552. Callable cb = get_singleton()->input_event_callback;
  553. if (!cb.is_null()) {
  554. Variant ev = p_event;
  555. Variant *evp = &ev;
  556. Variant ret;
  557. Callable::CallError ce;
  558. cb.call((const Variant **)&evp, 1, ret, ce);
  559. }
  560. }
  561. 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) {
  562. return memnew(DisplayServerJavaScript(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  563. }
  564. 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) {
  565. r_error = OK; // Always succeeds for now.
  566. // Ensure the canvas ID.
  567. godot_js_config_canvas_id_get(canvas_id, 256);
  568. // Handle contextmenu, webglcontextlost
  569. 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);
  570. // Check if it's windows.
  571. swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1;
  572. // Expose method for requesting quit.
  573. godot_js_os_request_quit_cb(request_quit_callback);
  574. #ifdef GLES3_ENABLED
  575. // TODO "vulkan" defaults to webgl2 for now.
  576. bool wants_webgl2 = p_rendering_driver == "opengl3" || p_rendering_driver == "vulkan";
  577. bool webgl2_init_failed = wants_webgl2 && !godot_js_display_has_webgl(2);
  578. if (wants_webgl2 && !webgl2_init_failed) {
  579. EmscriptenWebGLContextAttributes attributes;
  580. emscripten_webgl_init_context_attributes(&attributes);
  581. //attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
  582. attributes.alpha = true;
  583. attributes.antialias = false;
  584. attributes.majorVersion = 2;
  585. webgl_ctx = emscripten_webgl_create_context(canvas_id, &attributes);
  586. if (emscripten_webgl_make_context_current(webgl_ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
  587. webgl2_init_failed = true;
  588. } else {
  589. RasterizerGLES3::make_current();
  590. }
  591. }
  592. if (webgl2_init_failed) {
  593. OS::get_singleton()->alert("Your browser does not seem to support WebGL2. Please update your browser version.",
  594. "Unable to initialize video driver");
  595. }
  596. if (!wants_webgl2 || webgl2_init_failed) {
  597. RasterizerDummy::make_current();
  598. }
  599. #else
  600. RasterizerDummy::make_current();
  601. #endif
  602. // JS Input interface (js/libs/library_godot_input.js)
  603. godot_js_input_mouse_button_cb(&DisplayServerJavaScript::mouse_button_callback);
  604. godot_js_input_mouse_move_cb(&DisplayServerJavaScript::mouse_move_callback);
  605. godot_js_input_mouse_wheel_cb(&DisplayServerJavaScript::mouse_wheel_callback);
  606. godot_js_input_touch_cb(&DisplayServerJavaScript::touch_callback, touch_event.identifier, touch_event.coords);
  607. godot_js_input_key_cb(&DisplayServerJavaScript::key_callback, key_event.code, key_event.key);
  608. godot_js_input_paste_cb(update_clipboard_callback);
  609. godot_js_input_drop_files_cb(drop_files_js_callback);
  610. godot_js_input_gamepad_cb(&DisplayServerJavaScript::gamepad_callback);
  611. // JS Display interface (js/libs/library_godot_display.js)
  612. godot_js_display_fullscreen_cb(&DisplayServerJavaScript::fullscreen_change_callback);
  613. godot_js_display_window_blur_cb(&window_blur_callback);
  614. godot_js_display_notification_cb(&send_window_event_callback,
  615. WINDOW_EVENT_MOUSE_ENTER,
  616. WINDOW_EVENT_MOUSE_EXIT,
  617. WINDOW_EVENT_FOCUS_IN,
  618. WINDOW_EVENT_FOCUS_OUT);
  619. godot_js_display_vk_cb(&vk_input_text_callback);
  620. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
  621. }
  622. DisplayServerJavaScript::~DisplayServerJavaScript() {
  623. #ifdef GLES3_ENABLED
  624. if (webgl_ctx) {
  625. emscripten_webgl_commit_frame();
  626. emscripten_webgl_destroy_context(webgl_ctx);
  627. }
  628. #endif
  629. }
  630. bool DisplayServerJavaScript::has_feature(Feature p_feature) const {
  631. switch (p_feature) {
  632. //case FEATURE_GLOBAL_MENU:
  633. //case FEATURE_HIDPI:
  634. //case FEATURE_IME:
  635. case FEATURE_ICON:
  636. case FEATURE_CLIPBOARD:
  637. case FEATURE_CURSOR_SHAPE:
  638. case FEATURE_CUSTOM_CURSOR_SHAPE:
  639. case FEATURE_MOUSE:
  640. case FEATURE_TOUCHSCREEN:
  641. return true;
  642. //case FEATURE_MOUSE_WARP:
  643. //case FEATURE_NATIVE_DIALOG:
  644. //case FEATURE_NATIVE_ICON:
  645. //case FEATURE_WINDOW_TRANSPARENCY:
  646. //case FEATURE_KEEP_SCREEN_ON:
  647. //case FEATURE_ORIENTATION:
  648. case FEATURE_VIRTUAL_KEYBOARD:
  649. return godot_js_display_vk_available() != 0;
  650. default:
  651. return false;
  652. }
  653. }
  654. void DisplayServerJavaScript::register_javascript_driver() {
  655. register_create_function("javascript", create_func, get_rendering_drivers_func);
  656. }
  657. String DisplayServerJavaScript::get_name() const {
  658. return "javascript";
  659. }
  660. int DisplayServerJavaScript::get_screen_count() const {
  661. return 1;
  662. }
  663. Point2i DisplayServerJavaScript::screen_get_position(int p_screen) const {
  664. return Point2i(); // TODO offsetX/Y?
  665. }
  666. Size2i DisplayServerJavaScript::screen_get_size(int p_screen) const {
  667. int size[2];
  668. godot_js_display_screen_size_get(size, size + 1);
  669. return Size2(size[0], size[1]);
  670. }
  671. Rect2i DisplayServerJavaScript::screen_get_usable_rect(int p_screen) const {
  672. int size[2];
  673. godot_js_display_window_size_get(size, size + 1);
  674. return Rect2i(0, 0, size[0], size[1]);
  675. }
  676. int DisplayServerJavaScript::screen_get_dpi(int p_screen) const {
  677. return godot_js_display_screen_dpi_get();
  678. }
  679. float DisplayServerJavaScript::screen_get_scale(int p_screen) const {
  680. return godot_js_display_pixel_ratio_get();
  681. }
  682. float DisplayServerJavaScript::screen_get_refresh_rate(int p_screen) const {
  683. 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.
  684. }
  685. Vector<DisplayServer::WindowID> DisplayServerJavaScript::get_window_list() const {
  686. Vector<WindowID> ret;
  687. ret.push_back(MAIN_WINDOW_ID);
  688. return ret;
  689. }
  690. DisplayServerJavaScript::WindowID DisplayServerJavaScript::get_window_at_screen_position(const Point2i &p_position) const {
  691. return MAIN_WINDOW_ID;
  692. }
  693. void DisplayServerJavaScript::window_attach_instance_id(ObjectID p_instance, WindowID p_window) {
  694. window_attached_instance_id = p_instance;
  695. }
  696. ObjectID DisplayServerJavaScript::window_get_attached_instance_id(WindowID p_window) const {
  697. return window_attached_instance_id;
  698. }
  699. void DisplayServerJavaScript::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) {
  700. // Not supported.
  701. }
  702. void DisplayServerJavaScript::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) {
  703. window_event_callback = p_callable;
  704. }
  705. void DisplayServerJavaScript::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) {
  706. input_event_callback = p_callable;
  707. }
  708. void DisplayServerJavaScript::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) {
  709. input_text_callback = p_callable;
  710. }
  711. void DisplayServerJavaScript::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) {
  712. drop_files_callback = p_callable;
  713. }
  714. void DisplayServerJavaScript::window_set_title(const String &p_title, WindowID p_window) {
  715. godot_js_display_window_title_set(p_title.utf8().get_data());
  716. }
  717. int DisplayServerJavaScript::window_get_current_screen(WindowID p_window) const {
  718. return 1;
  719. }
  720. void DisplayServerJavaScript::window_set_current_screen(int p_screen, WindowID p_window) {
  721. // Not implemented.
  722. }
  723. Point2i DisplayServerJavaScript::window_get_position(WindowID p_window) const {
  724. return Point2i(); // TODO Does this need implementation?
  725. }
  726. void DisplayServerJavaScript::window_set_position(const Point2i &p_position, WindowID p_window) {
  727. // Not supported.
  728. }
  729. void DisplayServerJavaScript::window_set_transient(WindowID p_window, WindowID p_parent) {
  730. // Not supported.
  731. }
  732. void DisplayServerJavaScript::window_set_max_size(const Size2i p_size, WindowID p_window) {
  733. // Not supported.
  734. }
  735. Size2i DisplayServerJavaScript::window_get_max_size(WindowID p_window) const {
  736. return Size2i();
  737. }
  738. void DisplayServerJavaScript::window_set_min_size(const Size2i p_size, WindowID p_window) {
  739. // Not supported.
  740. }
  741. Size2i DisplayServerJavaScript::window_get_min_size(WindowID p_window) const {
  742. return Size2i();
  743. }
  744. void DisplayServerJavaScript::window_set_size(const Size2i p_size, WindowID p_window) {
  745. godot_js_display_desired_size_set(p_size.x, p_size.y);
  746. }
  747. Size2i DisplayServerJavaScript::window_get_size(WindowID p_window) const {
  748. int size[2];
  749. godot_js_display_window_size_get(size, size + 1);
  750. return Size2i(size[0], size[1]);
  751. }
  752. Size2i DisplayServerJavaScript::window_get_real_size(WindowID p_window) const {
  753. return window_get_size(p_window);
  754. }
  755. void DisplayServerJavaScript::window_set_mode(WindowMode p_mode, WindowID p_window) {
  756. if (window_mode == p_mode) {
  757. return;
  758. }
  759. switch (p_mode) {
  760. case WINDOW_MODE_WINDOWED: {
  761. if (window_mode == WINDOW_MODE_FULLSCREEN) {
  762. godot_js_display_fullscreen_exit();
  763. }
  764. window_mode = WINDOW_MODE_WINDOWED;
  765. } break;
  766. case WINDOW_MODE_EXCLUSIVE_FULLSCREEN:
  767. case WINDOW_MODE_FULLSCREEN: {
  768. int result = godot_js_display_fullscreen_request();
  769. ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
  770. } break;
  771. case WINDOW_MODE_MAXIMIZED:
  772. case WINDOW_MODE_MINIMIZED:
  773. WARN_PRINT("WindowMode MAXIMIZED and MINIMIZED are not supported in HTML5 platform.");
  774. break;
  775. default:
  776. break;
  777. }
  778. }
  779. DisplayServerJavaScript::WindowMode DisplayServerJavaScript::window_get_mode(WindowID p_window) const {
  780. return window_mode;
  781. }
  782. bool DisplayServerJavaScript::window_is_maximize_allowed(WindowID p_window) const {
  783. return false;
  784. }
  785. void DisplayServerJavaScript::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  786. // Not supported.
  787. }
  788. bool DisplayServerJavaScript::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  789. return false;
  790. }
  791. void DisplayServerJavaScript::window_request_attention(WindowID p_window) {
  792. // Not supported.
  793. }
  794. void DisplayServerJavaScript::window_move_to_foreground(WindowID p_window) {
  795. // Not supported.
  796. }
  797. bool DisplayServerJavaScript::window_can_draw(WindowID p_window) const {
  798. return true;
  799. }
  800. bool DisplayServerJavaScript::can_any_window_draw() const {
  801. return true;
  802. }
  803. void DisplayServerJavaScript::process_events() {
  804. Input::get_singleton()->flush_buffered_events();
  805. if (godot_js_input_gamepad_sample() == OK) {
  806. process_joypads();
  807. }
  808. }
  809. int DisplayServerJavaScript::get_current_video_driver() const {
  810. return 1;
  811. }
  812. bool DisplayServerJavaScript::get_swap_cancel_ok() {
  813. return swap_cancel_ok;
  814. }
  815. void DisplayServerJavaScript::swap_buffers() {
  816. #ifdef GLES3_ENABLED
  817. if (webgl_ctx) {
  818. emscripten_webgl_commit_frame();
  819. }
  820. #endif
  821. }