os_javascript.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*************************************************************************/
  2. /* os_javascript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os_javascript.h"
  31. #include "core/global_config.h"
  32. #include "core/io/file_access_buffered_fa.h"
  33. #include "dom_keys.h"
  34. #include "drivers/gles3/rasterizer_gles3.h"
  35. #include "drivers/unix/dir_access_unix.h"
  36. #include "drivers/unix/file_access_unix.h"
  37. #include "main/main.h"
  38. #include "servers/visual/visual_server_raster.h"
  39. #include <emscripten.h>
  40. #include <stdlib.h>
  41. int OS_JavaScript::get_video_driver_count() const {
  42. return 1;
  43. }
  44. const char *OS_JavaScript::get_video_driver_name(int p_driver) const {
  45. return "GLES3";
  46. }
  47. OS::VideoMode OS_JavaScript::get_default_video_mode() const {
  48. return OS::VideoMode();
  49. }
  50. int OS_JavaScript::get_audio_driver_count() const {
  51. return 1;
  52. }
  53. const char *OS_JavaScript::get_audio_driver_name(int p_driver) const {
  54. return "JavaScript";
  55. }
  56. void OS_JavaScript::initialize_core() {
  57. OS_Unix::initialize_core();
  58. FileAccess::make_default<FileAccessBufferedFA<FileAccessUnix> >(FileAccess::ACCESS_RESOURCES);
  59. }
  60. void OS_JavaScript::set_opengl_extensions(const char *p_gl_extensions) {
  61. ERR_FAIL_COND(!p_gl_extensions);
  62. gl_extensions = p_gl_extensions;
  63. }
  64. static EM_BOOL _browser_resize_callback(int event_type, const EmscriptenUiEvent *ui_event, void *user_data) {
  65. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_RESIZE, false);
  66. OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
  67. // the order in which _browser_resize_callback and
  68. // _fullscreen_change_callback are called is browser-dependent,
  69. // so try adjusting for fullscreen in both
  70. if (os->is_window_fullscreen() || os->is_window_maximized()) {
  71. OS::VideoMode vm = os->get_video_mode();
  72. vm.width = ui_event->windowInnerWidth;
  73. vm.height = ui_event->windowInnerHeight;
  74. os->set_video_mode(vm);
  75. emscripten_set_canvas_size(ui_event->windowInnerWidth, ui_event->windowInnerHeight);
  76. }
  77. return false;
  78. }
  79. static Size2 _windowed_size;
  80. static EM_BOOL _fullscreen_change_callback(int event_type, const EmscriptenFullscreenChangeEvent *event, void *user_data) {
  81. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_FULLSCREENCHANGE, false);
  82. OS_JavaScript *os = static_cast<OS_JavaScript *>(user_data);
  83. String id = String::utf8(event->id);
  84. // empty id is canvas
  85. if (id.empty() || id == "canvas") {
  86. OS::VideoMode vm = os->get_video_mode();
  87. // this event property is the only reliable information on
  88. // browser fullscreen state
  89. vm.fullscreen = event->isFullscreen;
  90. if (event->isFullscreen) {
  91. vm.width = event->screenWidth;
  92. vm.height = event->screenHeight;
  93. os->set_video_mode(vm);
  94. emscripten_set_canvas_size(vm.width, vm.height);
  95. } else {
  96. os->set_video_mode(vm);
  97. if (!os->is_window_maximized()) {
  98. os->set_window_size(_windowed_size);
  99. }
  100. }
  101. }
  102. return false;
  103. }
  104. static InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
  105. InputEvent ev;
  106. ev.type = InputEvent::KEY;
  107. ev.key.echo = emscripten_event->repeat;
  108. ev.key.mod.alt = emscripten_event->altKey;
  109. ev.key.mod.shift = emscripten_event->shiftKey;
  110. ev.key.mod.control = emscripten_event->ctrlKey;
  111. ev.key.mod.meta = emscripten_event->metaKey;
  112. ev.key.scancode = dom2godot_scancode(emscripten_event->keyCode);
  113. String unicode = String::utf8(emscripten_event->key);
  114. // check if empty or multi-character (e.g. `CapsLock`)
  115. if (unicode.length() != 1) {
  116. // might be empty as well, but better than nonsense
  117. unicode = String::utf8(emscripten_event->charValue);
  118. }
  119. if (unicode.length() == 1) {
  120. ev.key.unicode = unicode[0];
  121. }
  122. return ev;
  123. }
  124. static InputEvent deferred_key_event;
  125. static EM_BOOL _keydown_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  126. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYDOWN, false);
  127. InputEvent ev = _setup_key_event(key_event);
  128. ev.key.pressed = true;
  129. if (ev.key.unicode == 0 && keycode_has_unicode(ev.key.scancode)) {
  130. // defer to keypress event for legacy unicode retrieval
  131. deferred_key_event = ev;
  132. return false; // do not suppress keypress event
  133. }
  134. static_cast<OS_JavaScript *>(user_data)->push_input(ev);
  135. return true;
  136. }
  137. static EM_BOOL _keypress_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  138. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYPRESS, false);
  139. deferred_key_event.key.unicode = key_event->charCode;
  140. static_cast<OS_JavaScript *>(user_data)->push_input(deferred_key_event);
  141. return true;
  142. }
  143. static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  144. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYUP, false);
  145. InputEvent ev = _setup_key_event(key_event);
  146. ev.key.pressed = false;
  147. static_cast<OS_JavaScript *>(user_data)->push_input(ev);
  148. return ev.key.scancode != KEY_UNKNOWN && ev.key.scancode != 0;
  149. }
  150. static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) {
  151. OS_JavaScript *os = (OS_JavaScript *)OS::get_singleton();
  152. if (os) {
  153. return os->joy_connection_changed(p_type, p_event);
  154. }
  155. return false;
  156. }
  157. void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  158. print_line("Init OS");
  159. if (gfx_init_func)
  160. gfx_init_func(gfx_init_ud, use_gl2, p_desired.width, p_desired.height, p_desired.fullscreen);
  161. // nothing to do here, can't fulfil fullscreen request due to
  162. // browser security, window size is already set from HTML
  163. video_mode = p_desired;
  164. video_mode.fullscreen = false;
  165. _windowed_size = get_window_size();
  166. // find locale, emscripten only sets "C"
  167. char locale_ptr[16];
  168. /* clang-format off */
  169. EM_ASM_({
  170. var locale = "";
  171. if (Module.locale) {
  172. // best case: server-side script reads Accept-Language early and
  173. // defines the locale to be read here
  174. locale = Module.locale;
  175. } else {
  176. // no luck, use what the JS engine can tell us
  177. // if this turns out not compatible enough, add tests for
  178. // browserLanguage, systemLanguage and userLanguage
  179. locale = navigator.languages ? navigator.languages[0] : navigator.language;
  180. }
  181. locale = locale.split('.')[0];
  182. stringToUTF8(locale, $0, 16);
  183. }, locale_ptr);
  184. /* clang-format on */
  185. setenv("LANG", locale_ptr, true);
  186. print_line("Init Audio");
  187. AudioDriverManager::add_driver(&audio_driver_javascript);
  188. audio_driver_javascript.set_singleton();
  189. if (audio_driver_javascript.init() != OK) {
  190. ERR_PRINT("Initializing audio failed.");
  191. }
  192. RasterizerGLES3::register_config();
  193. RasterizerGLES3::make_current();
  194. print_line("Init VS");
  195. visual_server = memnew(VisualServerRaster());
  196. visual_server->cursor_set_visible(false, 0);
  197. print_line("Init Physicsserver");
  198. physics_server = memnew(PhysicsServerSW);
  199. physics_server->init();
  200. physics_2d_server = memnew(Physics2DServerSW);
  201. physics_2d_server->init();
  202. input = memnew(InputDefault);
  203. power_manager = memnew(PowerJavascript);
  204. #define EM_CHECK(ev) \
  205. if (result != EMSCRIPTEN_RESULT_SUCCESS) \
  206. ERR_PRINTS("Error while setting " #ev " callback: Code " + itos(result))
  207. #define SET_EM_CALLBACK(ev, cb) \
  208. result = emscripten_set_##ev##_callback(NULL, this, true, &cb); \
  209. EM_CHECK(ev)
  210. #define SET_EM_CALLBACK_NODATA(ev, cb) \
  211. result = emscripten_set_##ev##_callback(NULL, true, &cb); \
  212. EM_CHECK(ev)
  213. EMSCRIPTEN_RESULT result;
  214. SET_EM_CALLBACK(keydown, _keydown_callback)
  215. SET_EM_CALLBACK(keypress, _keypress_callback)
  216. SET_EM_CALLBACK(keyup, _keyup_callback)
  217. SET_EM_CALLBACK(resize, _browser_resize_callback)
  218. SET_EM_CALLBACK(fullscreenchange, _fullscreen_change_callback)
  219. SET_EM_CALLBACK_NODATA(gamepadconnected, joy_callback_func)
  220. SET_EM_CALLBACK_NODATA(gamepaddisconnected, joy_callback_func)
  221. #undef SET_EM_CALLBACK_NODATA
  222. #undef SET_EM_CALLBACK
  223. #undef EM_CHECK
  224. #ifdef JAVASCRIPT_EVAL_ENABLED
  225. javascript_eval = memnew(JavaScript);
  226. GlobalConfig::get_singleton()->add_singleton(GlobalConfig::Singleton("JavaScript", javascript_eval));
  227. #endif
  228. visual_server->init();
  229. }
  230. void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
  231. main_loop = p_main_loop;
  232. input->set_main_loop(p_main_loop);
  233. }
  234. void OS_JavaScript::delete_main_loop() {
  235. memdelete(main_loop);
  236. }
  237. void OS_JavaScript::finalize() {
  238. memdelete(input);
  239. }
  240. void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
  241. /* clang-format off */
  242. EM_ASM_({
  243. window.alert(UTF8ToString($0));
  244. }, p_alert.utf8().get_data());
  245. /* clang-format on */
  246. }
  247. void OS_JavaScript::set_mouse_show(bool p_show) {
  248. //javascript has no mouse...
  249. }
  250. void OS_JavaScript::set_mouse_grab(bool p_grab) {
  251. //it really has no mouse...!
  252. }
  253. bool OS_JavaScript::is_mouse_grab_enabled() const {
  254. //*sigh* technology has evolved so much since i was a kid..
  255. return false;
  256. }
  257. Point2 OS_JavaScript::get_mouse_position() const {
  258. return input->get_mouse_position();
  259. }
  260. int OS_JavaScript::get_mouse_button_state() const {
  261. return last_button_mask;
  262. }
  263. void OS_JavaScript::set_window_title(const String &p_title) {
  264. /* clang-format off */
  265. EM_ASM_({
  266. document.title = UTF8ToString($0);
  267. }, p_title.utf8().get_data());
  268. /* clang-format on */
  269. }
  270. //interesting byt not yet
  271. //void set_clipboard(const String& p_text);
  272. //String get_clipboard() const;
  273. void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  274. video_mode = p_video_mode;
  275. }
  276. OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
  277. return video_mode;
  278. }
  279. Size2 OS_JavaScript::get_screen_size(int p_screen) const {
  280. ERR_FAIL_COND_V(p_screen != 0, Size2());
  281. EmscriptenFullscreenChangeEvent ev;
  282. EMSCRIPTEN_RESULT result = emscripten_get_fullscreen_status(&ev);
  283. ERR_FAIL_COND_V(result != EMSCRIPTEN_RESULT_SUCCESS, Size2());
  284. return Size2(ev.screenWidth, ev.screenHeight);
  285. }
  286. void OS_JavaScript::set_window_size(const Size2 p_size) {
  287. window_maximized = false;
  288. if (is_window_fullscreen()) {
  289. set_window_fullscreen(false);
  290. }
  291. _windowed_size = p_size;
  292. video_mode.width = p_size.x;
  293. video_mode.height = p_size.y;
  294. emscripten_set_canvas_size(p_size.x, p_size.y);
  295. }
  296. Size2 OS_JavaScript::get_window_size() const {
  297. int canvas[3];
  298. emscripten_get_canvas_size(canvas, canvas + 1, canvas + 2);
  299. return Size2(canvas[0], canvas[1]);
  300. }
  301. void OS_JavaScript::set_window_maximized(bool p_enabled) {
  302. window_maximized = p_enabled;
  303. if (p_enabled) {
  304. if (is_window_fullscreen()) {
  305. // _browser_resize callback will set canvas size
  306. set_window_fullscreen(false);
  307. } else {
  308. /* clang-format off */
  309. video_mode.width = EM_ASM_INT_V(return window.innerWidth);
  310. video_mode.height = EM_ASM_INT_V(return window.innerHeight);
  311. /* clang-format on */
  312. emscripten_set_canvas_size(video_mode.width, video_mode.height);
  313. }
  314. } else {
  315. set_window_size(_windowed_size);
  316. }
  317. }
  318. void OS_JavaScript::set_window_fullscreen(bool p_enable) {
  319. if (p_enable == is_window_fullscreen()) {
  320. return;
  321. }
  322. // only requesting changes here, if successful, canvas is resized in
  323. // _browser_resize_callback or _fullscreen_change_callback
  324. EMSCRIPTEN_RESULT result;
  325. if (p_enable) {
  326. /* clang-format off */
  327. EM_ASM(Module.requestFullscreen(false, false););
  328. /* clang-format on */
  329. } else {
  330. result = emscripten_exit_fullscreen();
  331. if (result != EMSCRIPTEN_RESULT_SUCCESS) {
  332. ERR_PRINTS("Failed to exit fullscreen: Code " + itos(result));
  333. }
  334. }
  335. }
  336. bool OS_JavaScript::is_window_fullscreen() const {
  337. return video_mode.fullscreen;
  338. }
  339. void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  340. Size2 screen = get_screen_size();
  341. p_list->push_back(OS::VideoMode(screen.width, screen.height, true));
  342. }
  343. String OS_JavaScript::get_name() {
  344. return "HTML5";
  345. }
  346. MainLoop *OS_JavaScript::get_main_loop() const {
  347. return main_loop;
  348. }
  349. bool OS_JavaScript::can_draw() const {
  350. return true; //always?
  351. }
  352. void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
  353. //javascript really really really has no mouse.. how amazing..
  354. }
  355. void OS_JavaScript::main_loop_begin() {
  356. if (main_loop)
  357. main_loop->init();
  358. }
  359. bool OS_JavaScript::main_loop_iterate() {
  360. if (!main_loop)
  361. return false;
  362. if (time_to_save_sync >= 0) {
  363. int64_t newtime = get_ticks_msec();
  364. int64_t elapsed = newtime - last_sync_time;
  365. last_sync_time = newtime;
  366. time_to_save_sync -= elapsed;
  367. if (time_to_save_sync < 0) {
  368. //time to sync, for real
  369. /* clang-format off */
  370. EM_ASM(
  371. FS.syncfs(function(err) {
  372. if (err) { Module.printErr('Failed to save IDB file system: ' + err.message); }
  373. });
  374. );
  375. /* clang-format on */
  376. }
  377. }
  378. process_joypads();
  379. return Main::iteration();
  380. }
  381. void OS_JavaScript::main_loop_end() {
  382. if (main_loop)
  383. main_loop->finish();
  384. }
  385. void OS_JavaScript::main_loop_focusout() {
  386. if (main_loop)
  387. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  388. //audio_driver_javascript.set_pause(true);
  389. }
  390. void OS_JavaScript::main_loop_focusin() {
  391. if (main_loop)
  392. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  393. //audio_driver_javascript.set_pause(false);
  394. }
  395. void OS_JavaScript::push_input(const InputEvent &p_ev) {
  396. InputEvent ev = p_ev;
  397. if (ev.type == InputEvent::MOUSE_MOTION) {
  398. input->set_mouse_position(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
  399. } else if (ev.type == InputEvent::MOUSE_BUTTON) {
  400. last_button_mask = ev.mouse_button.button_mask;
  401. }
  402. input->parse_input_event(p_ev);
  403. }
  404. void OS_JavaScript::process_touch(int p_what, int p_pointer, const Vector<TouchPos> &p_points) {
  405. //print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
  406. switch (p_what) {
  407. case 0: { //gesture begin
  408. if (touch.size()) {
  409. //end all if exist
  410. InputEvent ev;
  411. ev.type = InputEvent::MOUSE_BUTTON;
  412. ev.mouse_button.button_index = BUTTON_LEFT;
  413. ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
  414. ev.mouse_button.pressed = false;
  415. ev.mouse_button.x = touch[0].pos.x;
  416. ev.mouse_button.y = touch[0].pos.y;
  417. ev.mouse_button.global_x = touch[0].pos.x;
  418. ev.mouse_button.global_y = touch[0].pos.y;
  419. input->parse_input_event(ev);
  420. for (int i = 0; i < touch.size(); i++) {
  421. InputEvent ev;
  422. ev.type = InputEvent::SCREEN_TOUCH;
  423. ev.screen_touch.index = touch[i].id;
  424. ev.screen_touch.pressed = false;
  425. ev.screen_touch.x = touch[i].pos.x;
  426. ev.screen_touch.y = touch[i].pos.y;
  427. input->parse_input_event(ev);
  428. }
  429. }
  430. touch.resize(p_points.size());
  431. for (int i = 0; i < p_points.size(); i++) {
  432. touch[i].id = p_points[i].id;
  433. touch[i].pos = p_points[i].pos;
  434. }
  435. {
  436. //send mouse
  437. InputEvent ev;
  438. ev.type = InputEvent::MOUSE_BUTTON;
  439. ev.mouse_button.button_index = BUTTON_LEFT;
  440. ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
  441. ev.mouse_button.pressed = true;
  442. ev.mouse_button.x = touch[0].pos.x;
  443. ev.mouse_button.y = touch[0].pos.y;
  444. ev.mouse_button.global_x = touch[0].pos.x;
  445. ev.mouse_button.global_y = touch[0].pos.y;
  446. last_mouse = touch[0].pos;
  447. input->parse_input_event(ev);
  448. }
  449. //send touch
  450. for (int i = 0; i < touch.size(); i++) {
  451. InputEvent ev;
  452. ev.type = InputEvent::SCREEN_TOUCH;
  453. ev.screen_touch.index = touch[i].id;
  454. ev.screen_touch.pressed = true;
  455. ev.screen_touch.x = touch[i].pos.x;
  456. ev.screen_touch.y = touch[i].pos.y;
  457. input->parse_input_event(ev);
  458. }
  459. } break;
  460. case 1: { //motion
  461. if (p_points.size()) {
  462. //send mouse, should look for point 0?
  463. InputEvent ev;
  464. ev.type = InputEvent::MOUSE_MOTION;
  465. ev.mouse_motion.button_mask = BUTTON_MASK_LEFT;
  466. ev.mouse_motion.x = p_points[0].pos.x;
  467. ev.mouse_motion.y = p_points[0].pos.y;
  468. input->set_mouse_position(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
  469. ev.mouse_motion.speed_x = input->get_last_mouse_speed().x;
  470. ev.mouse_motion.speed_y = input->get_last_mouse_speed().y;
  471. ev.mouse_motion.relative_x = p_points[0].pos.x - last_mouse.x;
  472. ev.mouse_motion.relative_y = p_points[0].pos.y - last_mouse.y;
  473. last_mouse = p_points[0].pos;
  474. input->parse_input_event(ev);
  475. }
  476. ERR_FAIL_COND(touch.size() != p_points.size());
  477. for (int i = 0; i < touch.size(); i++) {
  478. int idx = -1;
  479. for (int j = 0; j < p_points.size(); j++) {
  480. if (touch[i].id == p_points[j].id) {
  481. idx = j;
  482. break;
  483. }
  484. }
  485. ERR_CONTINUE(idx == -1);
  486. if (touch[i].pos == p_points[idx].pos)
  487. continue; //no move unncesearily
  488. InputEvent ev;
  489. ev.type = InputEvent::SCREEN_DRAG;
  490. ev.screen_drag.index = touch[i].id;
  491. ev.screen_drag.x = p_points[idx].pos.x;
  492. ev.screen_drag.y = p_points[idx].pos.y;
  493. ev.screen_drag.relative_x = p_points[idx].pos.x - touch[i].pos.x;
  494. ev.screen_drag.relative_y = p_points[idx].pos.y - touch[i].pos.y;
  495. input->parse_input_event(ev);
  496. touch[i].pos = p_points[idx].pos;
  497. }
  498. } break;
  499. case 2: { //release
  500. if (touch.size()) {
  501. //end all if exist
  502. InputEvent ev;
  503. ev.type = InputEvent::MOUSE_BUTTON;
  504. ev.mouse_button.button_index = BUTTON_LEFT;
  505. ev.mouse_button.button_mask = BUTTON_MASK_LEFT;
  506. ev.mouse_button.pressed = false;
  507. ev.mouse_button.x = touch[0].pos.x;
  508. ev.mouse_button.y = touch[0].pos.y;
  509. ev.mouse_button.global_x = touch[0].pos.x;
  510. ev.mouse_button.global_y = touch[0].pos.y;
  511. input->parse_input_event(ev);
  512. for (int i = 0; i < touch.size(); i++) {
  513. InputEvent ev;
  514. ev.type = InputEvent::SCREEN_TOUCH;
  515. ev.screen_touch.index = touch[i].id;
  516. ev.screen_touch.pressed = false;
  517. ev.screen_touch.x = touch[i].pos.x;
  518. ev.screen_touch.y = touch[i].pos.y;
  519. input->parse_input_event(ev);
  520. }
  521. touch.clear();
  522. }
  523. } break;
  524. case 3: { // add tuchi
  525. ERR_FAIL_INDEX(p_pointer, p_points.size());
  526. TouchPos tp = p_points[p_pointer];
  527. touch.push_back(tp);
  528. InputEvent ev;
  529. ev.type = InputEvent::SCREEN_TOUCH;
  530. ev.screen_touch.index = tp.id;
  531. ev.screen_touch.pressed = true;
  532. ev.screen_touch.x = tp.pos.x;
  533. ev.screen_touch.y = tp.pos.y;
  534. input->parse_input_event(ev);
  535. } break;
  536. case 4: {
  537. for (int i = 0; i < touch.size(); i++) {
  538. if (touch[i].id == p_pointer) {
  539. InputEvent ev;
  540. ev.type = InputEvent::SCREEN_TOUCH;
  541. ev.screen_touch.index = touch[i].id;
  542. ev.screen_touch.pressed = false;
  543. ev.screen_touch.x = touch[i].pos.x;
  544. ev.screen_touch.y = touch[i].pos.y;
  545. input->parse_input_event(ev);
  546. touch.remove(i);
  547. i--;
  548. }
  549. }
  550. } break;
  551. }
  552. }
  553. void OS_JavaScript::process_accelerometer(const Vector3 &p_accelerometer) {
  554. input->set_accelerometer(p_accelerometer);
  555. }
  556. bool OS_JavaScript::has_touchscreen_ui_hint() const {
  557. return false; //???
  558. }
  559. void OS_JavaScript::main_loop_request_quit() {
  560. if (main_loop)
  561. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  562. }
  563. Error OS_JavaScript::shell_open(String p_uri) {
  564. /* clang-format off */
  565. EM_ASM_({
  566. window.open(UTF8ToString($0), '_blank');
  567. }, p_uri.utf8().get_data());
  568. /* clang-format on */
  569. return OK;
  570. }
  571. String OS_JavaScript::get_resource_dir() const {
  572. return "/"; //javascript has it's own filesystem for resources inside the APK
  573. }
  574. String OS_JavaScript::get_data_dir() const {
  575. /*
  576. if (get_data_dir_func)
  577. return get_data_dir_func();
  578. */
  579. return "/userfs";
  580. //return GlobalConfig::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
  581. };
  582. String OS_JavaScript::get_executable_path() const {
  583. return OS::get_executable_path();
  584. }
  585. void OS_JavaScript::_close_notification_funcs(const String &p_file, int p_flags) {
  586. print_line("close " + p_file + " flags " + itos(p_flags));
  587. if (p_file.begins_with("/userfs") && p_flags & FileAccess::WRITE) {
  588. static_cast<OS_JavaScript *>(get_singleton())->last_sync_time = OS::get_singleton()->get_ticks_msec();
  589. static_cast<OS_JavaScript *>(get_singleton())->time_to_save_sync = 5000; //five seconds since last save
  590. }
  591. }
  592. void OS_JavaScript::process_joypads() {
  593. int joy_count = emscripten_get_num_gamepads();
  594. for (int i = 0; i < joy_count; i++) {
  595. EmscriptenGamepadEvent state;
  596. emscripten_get_gamepad_status(i, &state);
  597. if (state.connected) {
  598. int num_buttons = MIN(state.numButtons, 18);
  599. int num_axes = MIN(state.numAxes, 8);
  600. for (int j = 0; j < num_buttons; j++) {
  601. float value = state.analogButton[j];
  602. if (String(state.mapping) == "standard" && (j == 6 || j == 7)) {
  603. InputDefault::JoyAxis jx;
  604. jx.min = 0;
  605. jx.value = value;
  606. input->joy_axis(i, j, jx);
  607. } else {
  608. input->joy_button(i, j, value);
  609. }
  610. }
  611. for (int j = 0; j < num_axes; j++) {
  612. InputDefault::JoyAxis jx;
  613. jx.min = -1;
  614. jx.value = state.axis[j];
  615. input->joy_axis(i, j, jx);
  616. }
  617. }
  618. }
  619. }
  620. bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event) {
  621. if (p_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) {
  622. String guid = "";
  623. if (String(p_event->mapping) == "standard")
  624. guid = "Default HTML5 Gamepad";
  625. input->joy_connection_changed(p_event->index, true, String(p_event->id), guid);
  626. } else {
  627. input->joy_connection_changed(p_event->index, false, "");
  628. }
  629. return true;
  630. }
  631. bool OS_JavaScript::is_joy_known(int p_device) {
  632. return input->is_joy_mapped(p_device);
  633. }
  634. String OS_JavaScript::get_joy_guid(int p_device) const {
  635. return input->get_joy_guid_remapped(p_device);
  636. }
  637. PowerState OS_JavaScript::get_power_state() {
  638. return power_manager->get_power_state();
  639. }
  640. int OS_JavaScript::get_power_seconds_left() {
  641. return power_manager->get_power_seconds_left();
  642. }
  643. int OS_JavaScript::get_power_percent_left() {
  644. return power_manager->get_power_percent_left();
  645. }
  646. OS_JavaScript::OS_JavaScript(const char *p_execpath, GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) {
  647. set_cmdline(p_execpath, get_cmdline_args());
  648. gfx_init_func = p_gfx_init_func;
  649. gfx_init_ud = p_gfx_init_ud;
  650. last_button_mask = 0;
  651. main_loop = NULL;
  652. gl_extensions = NULL;
  653. window_maximized = false;
  654. get_data_dir_func = p_get_data_dir_func;
  655. FileAccessUnix::close_notification_func = _close_notification_funcs;
  656. time_to_save_sync = -1;
  657. }
  658. OS_JavaScript::~OS_JavaScript() {
  659. }