2
0

os_javascript.cpp 27 KB

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