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-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. godot_js_display_cursor_lock_set(0);
  320. } else if (p_mode == MOUSE_MODE_HIDDEN) {
  321. godot_js_display_cursor_set_visible(0);
  322. godot_js_display_cursor_lock_set(0);
  323. } else if (p_mode == MOUSE_MODE_CAPTURED) {
  324. godot_js_display_cursor_set_visible(1);
  325. godot_js_display_cursor_lock_set(1);
  326. }
  327. }
  328. DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const {
  329. if (godot_js_display_cursor_is_hidden()) {
  330. return MOUSE_MODE_HIDDEN;
  331. }
  332. if (godot_js_display_cursor_is_locked()) {
  333. return MOUSE_MODE_CAPTURED;
  334. }
  335. return MOUSE_MODE_VISIBLE;
  336. }
  337. // Wheel
  338. int DisplayServerJavaScript::mouse_wheel_callback(double p_delta_x, double p_delta_y) {
  339. if (!godot_js_display_canvas_is_focused()) {
  340. if (get_singleton()->cursor_inside_canvas) {
  341. godot_js_display_canvas_focus();
  342. } else {
  343. return false;
  344. }
  345. }
  346. Input *input = Input::get_singleton();
  347. Ref<InputEventMouseButton> ev;
  348. ev.instantiate();
  349. ev->set_position(input->get_mouse_position());
  350. ev->set_global_position(ev->get_position());
  351. ev->set_shift_pressed(input->is_key_pressed(KEY_SHIFT));
  352. ev->set_alt_pressed(input->is_key_pressed(KEY_ALT));
  353. ev->set_ctrl_pressed(input->is_key_pressed(KEY_CTRL));
  354. ev->set_meta_pressed(input->is_key_pressed(KEY_META));
  355. if (p_delta_y < 0) {
  356. ev->set_button_index(MOUSE_BUTTON_WHEEL_UP);
  357. } else if (p_delta_y > 0) {
  358. ev->set_button_index(MOUSE_BUTTON_WHEEL_DOWN);
  359. } else if (p_delta_x > 0) {
  360. ev->set_button_index(MOUSE_BUTTON_WHEEL_LEFT);
  361. } else if (p_delta_x < 0) {
  362. ev->set_button_index(MOUSE_BUTTON_WHEEL_RIGHT);
  363. } else {
  364. return false;
  365. }
  366. // Different browsers give wildly different delta values, and we can't
  367. // interpret deltaMode, so use default value for wheel events' factor.
  368. int button_flag = 1 << (ev->get_button_index() - 1);
  369. ev->set_pressed(true);
  370. ev->set_button_mask(input->get_mouse_button_mask() | button_flag);
  371. input->parse_input_event(ev);
  372. Ref<InputEventMouseButton> release = ev->duplicate();
  373. release->set_pressed(false);
  374. release->set_button_mask(MouseButton(input->get_mouse_button_mask() & ~button_flag));
  375. input->parse_input_event(release);
  376. return true;
  377. }
  378. // Touch
  379. void DisplayServerJavaScript::touch_callback(int p_type, int p_count) {
  380. DisplayServerJavaScript *ds = get_singleton();
  381. const JSTouchEvent &touch_event = ds->touch_event;
  382. for (int i = 0; i < p_count; i++) {
  383. Point2 point(touch_event.coords[i * 2], touch_event.coords[i * 2 + 1]);
  384. if (p_type == 2) {
  385. // touchmove
  386. Ref<InputEventScreenDrag> ev;
  387. ev.instantiate();
  388. ev->set_index(touch_event.identifier[i]);
  389. ev->set_position(point);
  390. Point2 &prev = ds->touches[i];
  391. ev->set_relative(ev->get_position() - prev);
  392. prev = ev->get_position();
  393. Input::get_singleton()->parse_input_event(ev);
  394. } else {
  395. // touchstart/touchend
  396. Ref<InputEventScreenTouch> ev;
  397. // Resume audio context after input in case autoplay was denied.
  398. OS_JavaScript::get_singleton()->resume_audio();
  399. ev.instantiate();
  400. ev->set_index(touch_event.identifier[i]);
  401. ev->set_position(point);
  402. ev->set_pressed(p_type == 0);
  403. ds->touches[i] = point;
  404. Input::get_singleton()->parse_input_event(ev);
  405. // Make sure to flush all events so we can call restricted APIs inside the event.
  406. Input::get_singleton()->flush_buffered_events();
  407. }
  408. }
  409. }
  410. bool DisplayServerJavaScript::screen_is_touchscreen(int p_screen) const {
  411. return godot_js_display_touchscreen_is_available();
  412. }
  413. // Virtual Keyboard
  414. void DisplayServerJavaScript::vk_input_text_callback(const char *p_text, int p_cursor) {
  415. DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton();
  416. if (!ds || ds->input_text_callback.is_null()) {
  417. return;
  418. }
  419. // Call input_text
  420. Variant event = String(p_text);
  421. Variant *eventp = &event;
  422. Variant ret;
  423. Callable::CallError ce;
  424. ds->input_text_callback.call((const Variant **)&eventp, 1, ret, ce);
  425. // Insert key right to reach position.
  426. Input *input = Input::get_singleton();
  427. Ref<InputEventKey> k;
  428. for (int i = 0; i < p_cursor; i++) {
  429. k.instantiate();
  430. k->set_pressed(true);
  431. k->set_echo(false);
  432. k->set_keycode(KEY_RIGHT);
  433. input->parse_input_event(k);
  434. k.instantiate();
  435. k->set_pressed(false);
  436. k->set_echo(false);
  437. k->set_keycode(KEY_RIGHT);
  438. input->parse_input_event(k);
  439. }
  440. }
  441. 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) {
  442. godot_js_display_vk_show(p_existing_text.utf8().get_data(), p_multiline, p_cursor_start, p_cursor_end);
  443. }
  444. void DisplayServerJavaScript::virtual_keyboard_hide() {
  445. godot_js_display_vk_hide();
  446. }
  447. void DisplayServerJavaScript::window_blur_callback() {
  448. Input::get_singleton()->release_pressed_events();
  449. }
  450. // Gamepad
  451. void DisplayServerJavaScript::gamepad_callback(int p_index, int p_connected, const char *p_id, const char *p_guid) {
  452. Input *input = Input::get_singleton();
  453. if (p_connected) {
  454. input->joy_connection_changed(p_index, true, String::utf8(p_id), String::utf8(p_guid));
  455. } else {
  456. input->joy_connection_changed(p_index, false, "");
  457. }
  458. }
  459. void DisplayServerJavaScript::process_joypads() {
  460. Input *input = Input::get_singleton();
  461. int32_t pads = godot_js_input_gamepad_sample_count();
  462. int32_t s_btns_num = 0;
  463. int32_t s_axes_num = 0;
  464. int32_t s_standard = 0;
  465. float s_btns[16];
  466. float s_axes[10];
  467. for (int idx = 0; idx < pads; idx++) {
  468. int err = godot_js_input_gamepad_sample_get(idx, s_btns, &s_btns_num, s_axes, &s_axes_num, &s_standard);
  469. if (err) {
  470. continue;
  471. }
  472. for (int b = 0; b < s_btns_num; b++) {
  473. float value = s_btns[b];
  474. // Buttons 6 and 7 in the standard mapping need to be
  475. // axis to be handled as JOY_AXIS_TRIGGER by Godot.
  476. if (s_standard && (b == 6 || b == 7)) {
  477. Input::JoyAxisValue joy_axis;
  478. joy_axis.min = 0;
  479. joy_axis.value = value;
  480. JoyAxis a = b == 6 ? JOY_AXIS_TRIGGER_LEFT : JOY_AXIS_TRIGGER_RIGHT;
  481. input->joy_axis(idx, a, joy_axis);
  482. } else {
  483. input->joy_button(idx, (JoyButton)b, value);
  484. }
  485. }
  486. for (int a = 0; a < s_axes_num; a++) {
  487. Input::JoyAxisValue joy_axis;
  488. joy_axis.min = -1;
  489. joy_axis.value = s_axes[a];
  490. input->joy_axis(idx, (JoyAxis)a, joy_axis);
  491. }
  492. }
  493. }
  494. Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() {
  495. Vector<String> drivers;
  496. drivers.push_back("dummy");
  497. return drivers;
  498. }
  499. // Clipboard
  500. void DisplayServerJavaScript::update_clipboard_callback(const char *p_text) {
  501. get_singleton()->clipboard = p_text;
  502. }
  503. void DisplayServerJavaScript::clipboard_set(const String &p_text) {
  504. clipboard = p_text;
  505. int err = godot_js_display_clipboard_set(p_text.utf8().get_data());
  506. ERR_FAIL_COND_MSG(err, "Clipboard API is not supported.");
  507. }
  508. String DisplayServerJavaScript::clipboard_get() const {
  509. godot_js_display_clipboard_get(update_clipboard_callback);
  510. return clipboard;
  511. }
  512. void DisplayServerJavaScript::send_window_event_callback(int p_notification) {
  513. DisplayServerJavaScript *ds = get_singleton();
  514. if (!ds) {
  515. return;
  516. }
  517. if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) {
  518. ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER;
  519. }
  520. if (!ds->window_event_callback.is_null()) {
  521. Variant event = int(p_notification);
  522. Variant *eventp = &event;
  523. Variant ret;
  524. Callable::CallError ce;
  525. ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce);
  526. }
  527. }
  528. void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) {
  529. ERR_FAIL_COND(p_icon.is_null());
  530. Ref<Image> icon = p_icon;
  531. if (icon->is_compressed()) {
  532. icon = icon->duplicate();
  533. ERR_FAIL_COND(icon->decompress() != OK);
  534. }
  535. if (icon->get_format() != Image::FORMAT_RGBA8) {
  536. if (icon == p_icon)
  537. icon = icon->duplicate();
  538. icon->convert(Image::FORMAT_RGBA8);
  539. }
  540. png_image png_meta;
  541. memset(&png_meta, 0, sizeof png_meta);
  542. png_meta.version = PNG_IMAGE_VERSION;
  543. png_meta.width = icon->get_width();
  544. png_meta.height = icon->get_height();
  545. png_meta.format = PNG_FORMAT_RGBA;
  546. PackedByteArray png;
  547. size_t len;
  548. PackedByteArray data = icon->get_data();
  549. ERR_FAIL_COND(!png_image_write_get_memory_size(png_meta, len, 0, data.ptr(), 0, nullptr));
  550. png.resize(len);
  551. ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr));
  552. godot_js_display_window_icon_set(png.ptr(), len);
  553. }
  554. void DisplayServerJavaScript::_dispatch_input_event(const Ref<InputEvent> &p_event) {
  555. Callable cb = get_singleton()->input_event_callback;
  556. if (!cb.is_null()) {
  557. Variant ev = p_event;
  558. Variant *evp = &ev;
  559. Variant ret;
  560. Callable::CallError ce;
  561. cb.call((const Variant **)&evp, 1, ret, ce);
  562. }
  563. }
  564. 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) {
  565. return memnew(DisplayServerJavaScript(p_rendering_driver, p_window_mode, p_vsync_mode, p_flags, p_resolution, r_error));
  566. }
  567. 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) {
  568. r_error = OK; // Always succeeds for now.
  569. // Ensure the canvas ID.
  570. godot_js_config_canvas_id_get(canvas_id, 256);
  571. // Handle contextmenu, webglcontextlost
  572. 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);
  573. // Check if it's windows.
  574. swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1;
  575. // Expose method for requesting quit.
  576. godot_js_os_request_quit_cb(request_quit_callback);
  577. RasterizerDummy::make_current(); // TODO OpenGL in Godot 4.0... or webgpu?
  578. #if 0
  579. EmscriptenWebGLContextAttributes attributes;
  580. emscripten_webgl_init_context_attributes(&attributes);
  581. attributes.alpha = GLOBAL_GET("display/window/per_pixel_transparency/allowed");
  582. attributes.antialias = false;
  583. ERR_FAIL_INDEX_V(p_video_driver, VIDEO_DRIVER_MAX, ERR_INVALID_PARAMETER);
  584. if (p_desired.layered) {
  585. set_window_per_pixel_transparency_enabled(true);
  586. }
  587. bool gl_initialization_error = false;
  588. if (RasterizerGLES3::is_viable() == OK) {
  589. attributes.majorVersion = 1;
  590. RasterizerGLES3::register_config();
  591. RasterizerGLES3::make_current();
  592. } else {
  593. gl_initialization_error = true;
  594. }
  595. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(canvas_id, &attributes);
  596. if (emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS) {
  597. gl_initialization_error = true;
  598. }
  599. if (gl_initialization_error) {
  600. OS::get_singleton()->alert("Your browser does not seem to support WebGL. Please update your browser version.",
  601. "Unable to initialize video driver");
  602. return ERR_UNAVAILABLE;
  603. }
  604. video_driver_index = p_video_driver;
  605. #endif
  606. // JS Input interface (js/libs/library_godot_input.js)
  607. godot_js_input_mouse_button_cb(&DisplayServerJavaScript::mouse_button_callback);
  608. godot_js_input_mouse_move_cb(&DisplayServerJavaScript::mouse_move_callback);
  609. godot_js_input_mouse_wheel_cb(&DisplayServerJavaScript::mouse_wheel_callback);
  610. godot_js_input_touch_cb(&DisplayServerJavaScript::touch_callback, touch_event.identifier, touch_event.coords);
  611. godot_js_input_key_cb(&DisplayServerJavaScript::key_callback, key_event.code, key_event.key);
  612. godot_js_input_paste_cb(update_clipboard_callback);
  613. godot_js_input_drop_files_cb(drop_files_js_callback);
  614. godot_js_input_gamepad_cb(&DisplayServerJavaScript::gamepad_callback);
  615. // JS Display interface (js/libs/library_godot_display.js)
  616. godot_js_display_fullscreen_cb(&DisplayServerJavaScript::fullscreen_change_callback);
  617. godot_js_display_window_blur_cb(&window_blur_callback);
  618. godot_js_display_notification_cb(&send_window_event_callback,
  619. WINDOW_EVENT_MOUSE_ENTER,
  620. WINDOW_EVENT_MOUSE_EXIT,
  621. WINDOW_EVENT_FOCUS_IN,
  622. WINDOW_EVENT_FOCUS_OUT);
  623. godot_js_display_vk_cb(&vk_input_text_callback);
  624. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event);
  625. }
  626. DisplayServerJavaScript::~DisplayServerJavaScript() {
  627. //emscripten_webgl_commit_frame();
  628. //emscripten_webgl_destroy_context(webgl_ctx);
  629. }
  630. bool DisplayServerJavaScript::has_feature(Feature p_feature) const {
  631. switch (p_feature) {
  632. //case FEATURE_CONSOLE_WINDOW:
  633. //case FEATURE_GLOBAL_MENU:
  634. //case FEATURE_HIDPI:
  635. //case FEATURE_IME:
  636. case FEATURE_ICON:
  637. case FEATURE_CLIPBOARD:
  638. case FEATURE_CURSOR_SHAPE:
  639. case FEATURE_CUSTOM_CURSOR_SHAPE:
  640. case FEATURE_MOUSE:
  641. case FEATURE_TOUCHSCREEN:
  642. return true;
  643. //case FEATURE_MOUSE_WARP:
  644. //case FEATURE_NATIVE_DIALOG:
  645. //case FEATURE_NATIVE_ICON:
  646. //case FEATURE_WINDOW_TRANSPARENCY:
  647. //case FEATURE_KEEP_SCREEN_ON:
  648. //case FEATURE_ORIENTATION:
  649. case FEATURE_VIRTUAL_KEYBOARD:
  650. return godot_js_display_vk_available() != 0;
  651. default:
  652. return false;
  653. }
  654. }
  655. void DisplayServerJavaScript::register_javascript_driver() {
  656. register_create_function("javascript", create_func, get_rendering_drivers_func);
  657. }
  658. String DisplayServerJavaScript::get_name() const {
  659. return "javascript";
  660. }
  661. int DisplayServerJavaScript::get_screen_count() const {
  662. return 1;
  663. }
  664. Point2i DisplayServerJavaScript::screen_get_position(int p_screen) const {
  665. return Point2i(); // TODO offsetX/Y?
  666. }
  667. Size2i DisplayServerJavaScript::screen_get_size(int p_screen) const {
  668. int size[2];
  669. godot_js_display_screen_size_get(size, size + 1);
  670. return Size2(size[0], size[1]);
  671. }
  672. Rect2i DisplayServerJavaScript::screen_get_usable_rect(int p_screen) const {
  673. int size[2];
  674. godot_js_display_window_size_get(size, size + 1);
  675. return Rect2i(0, 0, size[0], size[1]);
  676. }
  677. int DisplayServerJavaScript::screen_get_dpi(int p_screen) const {
  678. return godot_js_display_screen_dpi_get();
  679. }
  680. float DisplayServerJavaScript::screen_get_scale(int p_screen) const {
  681. return godot_js_display_pixel_ratio_get();
  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_FULLSCREEN: {
  764. int result = godot_js_display_fullscreen_request();
  765. ERR_FAIL_COND_MSG(result, "The request was denied. Remember that enabling fullscreen is only possible from an input callback for the HTML5 platform.");
  766. } break;
  767. case WINDOW_MODE_MAXIMIZED:
  768. case WINDOW_MODE_MINIMIZED:
  769. WARN_PRINT("WindowMode MAXIMIZED and MINIMIZED are not supported in HTML5 platform.");
  770. break;
  771. default:
  772. break;
  773. }
  774. }
  775. DisplayServerJavaScript::WindowMode DisplayServerJavaScript::window_get_mode(WindowID p_window) const {
  776. return window_mode;
  777. }
  778. bool DisplayServerJavaScript::window_is_maximize_allowed(WindowID p_window) const {
  779. return false;
  780. }
  781. void DisplayServerJavaScript::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) {
  782. // Not supported.
  783. }
  784. bool DisplayServerJavaScript::window_get_flag(WindowFlags p_flag, WindowID p_window) const {
  785. return false;
  786. }
  787. void DisplayServerJavaScript::window_request_attention(WindowID p_window) {
  788. // Not supported.
  789. }
  790. void DisplayServerJavaScript::window_move_to_foreground(WindowID p_window) {
  791. // Not supported.
  792. }
  793. bool DisplayServerJavaScript::window_can_draw(WindowID p_window) const {
  794. return true;
  795. }
  796. bool DisplayServerJavaScript::can_any_window_draw() const {
  797. return true;
  798. }
  799. void DisplayServerJavaScript::process_events() {
  800. Input::get_singleton()->flush_buffered_events();
  801. if (godot_js_input_gamepad_sample() == OK) {
  802. process_joypads();
  803. }
  804. }
  805. int DisplayServerJavaScript::get_current_video_driver() const {
  806. return 1;
  807. }
  808. bool DisplayServerJavaScript::get_swap_cancel_ok() {
  809. return swap_cancel_ok;
  810. }
  811. void DisplayServerJavaScript::swap_buffers() {
  812. //emscripten_webgl_commit_frame();
  813. }