os_javascript.cpp 24 KB

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