os_javascript.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*************************************************************************/
  2. /* os_javascript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://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/io/file_access_buffered_fa.h"
  32. #include "drivers/gles2/rasterizer_gles2.h"
  33. #include "drivers/unix/dir_access_unix.h"
  34. #include "drivers/unix/file_access_unix.h"
  35. #include "servers/visual/visual_server_raster.h"
  36. #include "main/main.h"
  37. #include "core/globals.h"
  38. #include "dom_keys.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 "GLES2";
  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 int last_button_mask;
  118. static EM_BOOL _mousebutton_callback(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data) {
  119. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_MOUSEDOWN && event_type != EMSCRIPTEN_EVENT_MOUSEUP, false);
  120. InputEvent ev;
  121. ev.type = InputEvent::MOUSE_BUTTON;
  122. ev.mouse_button.pressed = event_type == EMSCRIPTEN_EVENT_MOUSEDOWN;
  123. ev.mouse_button.global_x = ev.mouse_button.x = mouse_event->canvasX;
  124. ev.mouse_button.global_y = ev.mouse_button.y = mouse_event->canvasY;
  125. ev.mouse_button.mod = dom2godot_mod(mouse_event);
  126. switch (mouse_event->button) {
  127. case DOM_BUTTON_LEFT: ev.mouse_button.button_index = BUTTON_LEFT; break;
  128. case DOM_BUTTON_MIDDLE: ev.mouse_button.button_index = BUTTON_MIDDLE; break;
  129. case DOM_BUTTON_RIGHT: ev.mouse_button.button_index = BUTTON_RIGHT; break;
  130. default: return false;
  131. }
  132. ev.mouse_button.button_mask = last_button_mask;
  133. if (ev.mouse_button.pressed)
  134. ev.mouse_button.button_mask |= 1 << (ev.mouse_button.button_index - 1);
  135. else
  136. ev.mouse_button.button_mask &= ~(1 << (ev.mouse_button.button_index - 1));
  137. last_button_mask = ev.mouse_button.button_mask;
  138. _input->parse_input_event(ev);
  139. return true;
  140. }
  141. static EM_BOOL _mousemove_callback(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data) {
  142. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_MOUSEMOVE, false);
  143. InputEvent ev;
  144. ev.type = InputEvent::MOUSE_MOTION;
  145. ev.mouse_motion.mod = dom2godot_mod(mouse_event);
  146. ev.mouse_motion.button_mask = last_button_mask;
  147. ev.mouse_motion.global_x = ev.mouse_motion.x = mouse_event->canvasX;
  148. ev.mouse_motion.global_y = ev.mouse_motion.y = mouse_event->canvasY;
  149. ev.mouse_motion.relative_x = _input->get_mouse_pos().x - ev.mouse_motion.x;
  150. ev.mouse_motion.relative_y = _input->get_mouse_pos().y - ev.mouse_motion.y;
  151. _input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
  152. ev.mouse_motion.speed_x = _input->get_mouse_speed().x;
  153. ev.mouse_motion.speed_y = _input->get_mouse_speed().y;
  154. _input->parse_input_event(ev);
  155. return true;
  156. }
  157. static EM_BOOL _wheel_callback(int event_type, const EmscriptenWheelEvent *wheel_event, void *user_data) {
  158. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_WHEEL, false);
  159. InputEvent ev;
  160. ev.type = InputEvent::MOUSE_BUTTON;
  161. ev.mouse_button.button_mask = last_button_mask;
  162. ev.mouse_button.global_x = ev.mouse_button.x = _input->get_mouse_pos().x;
  163. ev.mouse_button.global_y = ev.mouse_button.y = _input->get_mouse_pos().y;
  164. ev.mouse_button.mod.shift = _input->is_key_pressed(KEY_SHIFT);
  165. ev.mouse_button.mod.alt = _input->is_key_pressed(KEY_ALT);
  166. ev.mouse_button.mod.control = _input->is_key_pressed(KEY_CONTROL);
  167. ev.mouse_button.mod.meta = _input->is_key_pressed(KEY_META);
  168. if (wheel_event->deltaY < 0)
  169. ev.mouse_button.button_index = BUTTON_WHEEL_UP;
  170. else if (wheel_event->deltaY > 0)
  171. ev.mouse_button.button_index = BUTTON_WHEEL_DOWN;
  172. else if (wheel_event->deltaX > 0)
  173. ev.mouse_button.button_index = BUTTON_WHEEL_LEFT;
  174. else if (wheel_event->deltaX < 0)
  175. ev.mouse_button.button_index = BUTTON_WHEEL_RIGHT;
  176. else
  177. return false;
  178. ev.mouse_button.pressed = true;
  179. _input->parse_input_event(ev);
  180. ev.mouse_button.pressed = false;
  181. _input->parse_input_event(ev);
  182. return true;
  183. }
  184. static Point2 _prev_touches[32];
  185. static EM_BOOL _touchpress_callback(int event_type, const EmscriptenTouchEvent *touch_event, void *user_data) {
  186. ERR_FAIL_COND_V(
  187. event_type != EMSCRIPTEN_EVENT_TOUCHSTART &&
  188. event_type != EMSCRIPTEN_EVENT_TOUCHEND &&
  189. event_type != EMSCRIPTEN_EVENT_TOUCHCANCEL,
  190. false);
  191. InputEvent ev;
  192. ev.type = InputEvent::SCREEN_TOUCH;
  193. int lowest_id_index = -1;
  194. for (int i = 0; i < touch_event->numTouches; ++i) {
  195. const EmscriptenTouchPoint &touch = touch_event->touches[i];
  196. if (lowest_id_index == -1 || touch.identifier < touch_event->touches[lowest_id_index].identifier)
  197. lowest_id_index = i;
  198. if (!touch.isChanged)
  199. continue;
  200. ev.screen_touch.index = touch.identifier;
  201. _prev_touches[i].x = ev.screen_touch.x = touch.canvasX;
  202. _prev_touches[i].y = ev.screen_touch.y = touch.canvasY;
  203. ev.screen_touch.pressed = event_type == EMSCRIPTEN_EVENT_TOUCHSTART;
  204. _input->parse_input_event(ev);
  205. }
  206. if (touch_event->touches[lowest_id_index].isChanged) {
  207. ev.type = InputEvent::MOUSE_BUTTON;
  208. ev.mouse_button.mod = dom2godot_mod(touch_event);
  209. ev.mouse_button.button_mask = _input->get_mouse_button_mask() >> 1;
  210. ev.mouse_button.global_x = ev.mouse_button.x = touch_event->touches[lowest_id_index].canvasX;
  211. ev.mouse_button.global_y = ev.mouse_button.y = touch_event->touches[lowest_id_index].canvasY;
  212. ev.mouse_button.button_index = BUTTON_LEFT;
  213. ev.mouse_button.pressed = event_type == EMSCRIPTEN_EVENT_TOUCHSTART;
  214. _input->parse_input_event(ev);
  215. }
  216. return true;
  217. }
  218. static EM_BOOL _touchmove_callback(int event_type, const EmscriptenTouchEvent *touch_event, void *user_data) {
  219. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_TOUCHMOVE, false);
  220. InputEvent ev;
  221. ev.type = InputEvent::SCREEN_DRAG;
  222. int lowest_id_index = -1;
  223. for (int i = 0; i < touch_event->numTouches; ++i) {
  224. const EmscriptenTouchPoint &touch = touch_event->touches[i];
  225. if (lowest_id_index == -1 || touch.identifier < touch_event->touches[lowest_id_index].identifier)
  226. lowest_id_index = i;
  227. if (!touch.isChanged)
  228. continue;
  229. ev.screen_drag.index = touch.identifier;
  230. ev.screen_drag.x = touch.canvasX;
  231. ev.screen_drag.y = touch.canvasY;
  232. Point2 &prev = _prev_touches[i];
  233. ev.screen_drag.relative_x = touch.canvasX - prev.x;
  234. ev.screen_drag.relative_y = touch.canvasY - prev.y;
  235. prev.x = ev.screen_drag.x;
  236. prev.y = ev.screen_drag.y;
  237. _input->parse_input_event(ev);
  238. }
  239. if (touch_event->touches[lowest_id_index].isChanged) {
  240. ev.type = InputEvent::MOUSE_MOTION;
  241. ev.mouse_motion.mod = dom2godot_mod(touch_event);
  242. ev.mouse_motion.button_mask = _input->get_mouse_button_mask() >> 1;
  243. ev.mouse_motion.global_x = ev.mouse_motion.x = touch_event->touches[lowest_id_index].canvasX;
  244. ev.mouse_motion.global_y = ev.mouse_motion.y = touch_event->touches[lowest_id_index].canvasY;
  245. ev.mouse_motion.relative_x = _input->get_mouse_pos().x - ev.mouse_motion.x;
  246. ev.mouse_motion.relative_y = _input->get_mouse_pos().y - ev.mouse_motion.y;
  247. _input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
  248. ev.mouse_motion.speed_x = _input->get_mouse_speed().x;
  249. ev.mouse_motion.speed_y = _input->get_mouse_speed().y;
  250. _input->parse_input_event(ev);
  251. }
  252. return true;
  253. }
  254. static InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
  255. InputEvent ev;
  256. ev.type = InputEvent::KEY;
  257. ev.key.echo = emscripten_event->repeat;
  258. ev.key.mod = dom2godot_mod(emscripten_event);
  259. ev.key.scancode = dom2godot_scancode(emscripten_event->keyCode);
  260. String unicode = String::utf8(emscripten_event->key);
  261. // check if empty or multi-character (e.g. `CapsLock`)
  262. if (unicode.length() != 1) {
  263. // might be empty as well, but better than nonsense
  264. unicode = String::utf8(emscripten_event->charValue);
  265. }
  266. if (unicode.length() == 1) {
  267. ev.key.unicode = unicode[0];
  268. }
  269. return ev;
  270. }
  271. static InputEvent deferred_key_event;
  272. static EM_BOOL _keydown_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  273. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYDOWN, false);
  274. InputEvent ev = _setup_key_event(key_event);
  275. ev.key.pressed = true;
  276. if (ev.key.unicode == 0 && keycode_has_unicode(ev.key.scancode)) {
  277. // defer to keypress event for legacy unicode retrieval
  278. deferred_key_event = ev;
  279. return false; // do not suppress keypress event
  280. }
  281. _input->parse_input_event(ev);
  282. return true;
  283. }
  284. static EM_BOOL _keypress_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  285. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYPRESS, false);
  286. deferred_key_event.key.unicode = key_event->charCode;
  287. _input->parse_input_event(deferred_key_event);
  288. return true;
  289. }
  290. static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  291. ERR_FAIL_COND_V(event_type != EMSCRIPTEN_EVENT_KEYUP, false);
  292. InputEvent ev = _setup_key_event(key_event);
  293. ev.key.pressed = false;
  294. _input->parse_input_event(ev);
  295. return ev.key.scancode != KEY_UNKNOWN && ev.key.scancode != 0;
  296. }
  297. static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) {
  298. OS_JavaScript *os = (OS_JavaScript *)OS::get_singleton();
  299. if (os) {
  300. return os->joy_connection_changed(p_type, p_event);
  301. }
  302. return false;
  303. }
  304. void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) {
  305. print_line("Init OS");
  306. if (gfx_init_func)
  307. gfx_init_func(gfx_init_ud, use_gl2, p_desired.width, p_desired.height, p_desired.fullscreen);
  308. // nothing to do here, can't fulfil fullscreen request due to
  309. // browser security, window size is already set from HTML
  310. video_mode = p_desired;
  311. video_mode.fullscreen = false;
  312. _windowed_size = get_window_size();
  313. // find locale, emscripten only sets "C"
  314. char locale_ptr[16];
  315. /* clang-format off */
  316. EM_ASM_({
  317. var locale = "";
  318. if (Module.locale) {
  319. // best case: server-side script reads Accept-Language early and
  320. // defines the locale to be read here
  321. locale = Module.locale;
  322. } else {
  323. // no luck, use what the JS engine can tell us
  324. // if this turns out not compatible enough, add tests for
  325. // browserLanguage, systemLanguage and userLanguage
  326. locale = navigator.languages ? navigator.languages[0] : navigator.language;
  327. }
  328. locale = locale.split('.')[0];
  329. stringToUTF8(locale, $0, 16);
  330. }, locale_ptr);
  331. /* clang-format on */
  332. setenv("LANG", locale_ptr, true);
  333. print_line("Init Audio");
  334. AudioDriverManagerSW::add_driver(&audio_driver_javascript);
  335. if (true) {
  336. RasterizerGLES2 *rasterizer_gles22 = memnew(RasterizerGLES2(false, false, false, false));
  337. rasterizer_gles22->set_use_framebuffers(false); //not supported by emscripten
  338. if (gl_extensions)
  339. rasterizer_gles22->set_extensions(gl_extensions);
  340. rasterizer = rasterizer_gles22;
  341. } else {
  342. // rasterizer = memnew( RasterizerGLES1(true, false) );
  343. }
  344. print_line("Init VS");
  345. visual_server = memnew(VisualServerRaster(rasterizer));
  346. visual_server->init();
  347. visual_server->cursor_set_visible(false, 0);
  348. /*AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
  349. if (AudioDriverManagerSW::get_driver(p_audio_driver)->init()!=OK) {
  350. ERR_PRINT("Initializing audio failed.");
  351. }*/
  352. print_line("Init SM");
  353. //sample_manager = memnew( SampleManagerMallocSW );
  354. audio_server = memnew(AudioServerJavascript);
  355. print_line("Init Mixer");
  356. //audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
  357. audio_server->init();
  358. print_line("Init SoundServer");
  359. spatial_sound_server = memnew(SpatialSoundServerSW);
  360. spatial_sound_server->init();
  361. print_line("Init SpatialSoundServer");
  362. spatial_sound_2d_server = memnew(SpatialSound2DServerSW);
  363. spatial_sound_2d_server->init();
  364. //
  365. print_line("Init Physicsserver");
  366. physics_server = memnew(PhysicsServerSW);
  367. physics_server->init();
  368. physics_2d_server = memnew(Physics2DServerSW);
  369. physics_2d_server->init();
  370. input = memnew(InputDefault);
  371. _input = input;
  372. #define EM_CHECK(ev) \
  373. if (result != EMSCRIPTEN_RESULT_SUCCESS) \
  374. ERR_PRINTS("Error while setting " #ev " callback: Code " + itos(result))
  375. #define SET_EM_CALLBACK(target, ev, cb) \
  376. result = emscripten_set_##ev##_callback(target, this, true, &cb); \
  377. EM_CHECK(ev)
  378. #define SET_EM_CALLBACK_NODATA(ev, cb) \
  379. result = emscripten_set_##ev##_callback(NULL, true, &cb); \
  380. EM_CHECK(ev)
  381. EMSCRIPTEN_RESULT result;
  382. SET_EM_CALLBACK("#canvas", mousemove, _mousemove_callback)
  383. SET_EM_CALLBACK("#canvas", mousedown, _mousebutton_callback)
  384. SET_EM_CALLBACK("#canvas", mouseup, _mousebutton_callback)
  385. SET_EM_CALLBACK("#canvas", wheel, _wheel_callback)
  386. SET_EM_CALLBACK("#canvas", touchstart, _touchpress_callback)
  387. SET_EM_CALLBACK("#canvas", touchmove, _touchmove_callback)
  388. SET_EM_CALLBACK("#canvas", touchend, _touchpress_callback)
  389. SET_EM_CALLBACK("#canvas", touchcancel, _touchpress_callback)
  390. SET_EM_CALLBACK("#canvas", keydown, _keydown_callback)
  391. SET_EM_CALLBACK("#canvas", keypress, _keypress_callback)
  392. SET_EM_CALLBACK("#canvas", keyup, _keyup_callback)
  393. SET_EM_CALLBACK(NULL, resize, _browser_resize_callback)
  394. SET_EM_CALLBACK(NULL, fullscreenchange, _fullscreen_change_callback)
  395. SET_EM_CALLBACK_NODATA(gamepadconnected, joy_callback_func)
  396. SET_EM_CALLBACK_NODATA(gamepaddisconnected, joy_callback_func)
  397. #undef SET_EM_CALLBACK_NODATA
  398. #undef SET_EM_CALLBACK
  399. #undef EM_CHECK
  400. }
  401. void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
  402. main_loop = p_main_loop;
  403. input->set_main_loop(p_main_loop);
  404. }
  405. void OS_JavaScript::delete_main_loop() {
  406. memdelete(main_loop);
  407. }
  408. void OS_JavaScript::finalize() {
  409. memdelete(input);
  410. }
  411. void OS_JavaScript::alert(const String &p_alert, const String &p_title) {
  412. /* clang-format off */
  413. EM_ASM_({
  414. window.alert(UTF8ToString($0));
  415. }, p_alert.utf8().get_data());
  416. /* clang-format on */
  417. }
  418. void OS_JavaScript::set_mouse_show(bool p_show) {
  419. //javascript has no mouse...
  420. }
  421. void OS_JavaScript::set_mouse_grab(bool p_grab) {
  422. //it really has no mouse...!
  423. }
  424. bool OS_JavaScript::is_mouse_grab_enabled() const {
  425. //*sigh* technology has evolved so much since i was a kid..
  426. return false;
  427. }
  428. Point2 OS_JavaScript::get_mouse_pos() const {
  429. return input->get_mouse_pos();
  430. }
  431. int OS_JavaScript::get_mouse_button_state() const {
  432. return last_button_mask;
  433. }
  434. void OS_JavaScript::set_window_title(const String &p_title) {
  435. /* clang-format off */
  436. EM_ASM_({
  437. document.title = UTF8ToString($0);
  438. }, p_title.utf8().get_data());
  439. /* clang-format on */
  440. }
  441. //interesting byt not yet
  442. //void set_clipboard(const String& p_text);
  443. //String get_clipboard() const;
  444. void OS_JavaScript::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
  445. video_mode = p_video_mode;
  446. }
  447. OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
  448. return video_mode;
  449. }
  450. Size2 OS_JavaScript::get_screen_size(int p_screen) const {
  451. ERR_FAIL_COND_V(p_screen != 0, Size2());
  452. EmscriptenFullscreenChangeEvent ev;
  453. EMSCRIPTEN_RESULT result = emscripten_get_fullscreen_status(&ev);
  454. ERR_FAIL_COND_V(result != EMSCRIPTEN_RESULT_SUCCESS, Size2());
  455. return Size2(ev.screenWidth, ev.screenHeight);
  456. }
  457. void OS_JavaScript::set_window_size(const Size2 p_size) {
  458. window_maximized = false;
  459. if (is_window_fullscreen()) {
  460. set_window_fullscreen(false);
  461. }
  462. _windowed_size = p_size;
  463. video_mode.width = p_size.x;
  464. video_mode.height = p_size.y;
  465. emscripten_set_canvas_size(p_size.x, p_size.y);
  466. }
  467. Size2 OS_JavaScript::get_window_size() const {
  468. int canvas[3];
  469. emscripten_get_canvas_size(canvas, canvas + 1, canvas + 2);
  470. return Size2(canvas[0], canvas[1]);
  471. }
  472. void OS_JavaScript::set_window_maximized(bool p_enabled) {
  473. window_maximized = p_enabled;
  474. if (p_enabled) {
  475. if (is_window_fullscreen()) {
  476. // _browser_resize callback will set canvas size
  477. set_window_fullscreen(false);
  478. } else {
  479. /* clang-format off */
  480. video_mode.width = EM_ASM_INT_V(return window.innerWidth);
  481. video_mode.height = EM_ASM_INT_V(return window.innerHeight);
  482. /* clang-format on */
  483. emscripten_set_canvas_size(video_mode.width, video_mode.height);
  484. }
  485. } else {
  486. set_window_size(_windowed_size);
  487. }
  488. }
  489. void OS_JavaScript::set_window_fullscreen(bool p_enable) {
  490. if (p_enable == is_window_fullscreen()) {
  491. return;
  492. }
  493. // only requesting changes here, if successful, canvas is resized in
  494. // _browser_resize_callback or _fullscreen_change_callback
  495. EMSCRIPTEN_RESULT result;
  496. if (p_enable) {
  497. /* clang-format off */
  498. EM_ASM(Module.requestFullscreen(false, false););
  499. /* clang-format on */
  500. } else {
  501. result = emscripten_exit_fullscreen();
  502. if (result != EMSCRIPTEN_RESULT_SUCCESS) {
  503. ERR_PRINTS("Failed to exit fullscreen: Code " + itos(result));
  504. }
  505. }
  506. }
  507. bool OS_JavaScript::is_window_fullscreen() const {
  508. return video_mode.fullscreen;
  509. }
  510. void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen) const {
  511. Size2 screen = get_screen_size();
  512. p_list->push_back(OS::VideoMode(screen.width, screen.height, true));
  513. }
  514. String OS_JavaScript::get_name() {
  515. return "HTML5";
  516. }
  517. MainLoop *OS_JavaScript::get_main_loop() const {
  518. return main_loop;
  519. }
  520. bool OS_JavaScript::can_draw() const {
  521. return true; //always?
  522. }
  523. void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
  524. //javascript really really really has no mouse.. how amazing..
  525. }
  526. void OS_JavaScript::main_loop_begin() {
  527. if (main_loop)
  528. main_loop->init();
  529. }
  530. bool OS_JavaScript::main_loop_iterate() {
  531. if (!main_loop)
  532. return false;
  533. if (time_to_save_sync >= 0) {
  534. int64_t newtime = get_ticks_msec();
  535. int64_t elapsed = newtime - last_sync_time;
  536. last_sync_time = newtime;
  537. time_to_save_sync -= elapsed;
  538. print_line("elapsed " + itos(elapsed) + " tts " + itos(time_to_save_sync));
  539. if (time_to_save_sync < 0) {
  540. //time to sync, for real
  541. /* clang-format off */
  542. EM_ASM(
  543. FS.syncfs(function (err) {
  544. assert(!err);
  545. console.log("Synched!");
  546. //ccall('success', 'v');
  547. });
  548. );
  549. /* clang-format on */
  550. }
  551. }
  552. process_joysticks();
  553. return Main::iteration();
  554. }
  555. void OS_JavaScript::main_loop_end() {
  556. if (main_loop)
  557. main_loop->finish();
  558. }
  559. void OS_JavaScript::main_loop_focusout() {
  560. if (main_loop)
  561. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  562. //audio_driver_javascript.set_pause(true);
  563. }
  564. void OS_JavaScript::main_loop_focusin() {
  565. if (main_loop)
  566. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  567. //audio_driver_javascript.set_pause(false);
  568. }
  569. void OS_JavaScript::process_accelerometer(const Vector3 &p_accelerometer) {
  570. input->set_accelerometer(p_accelerometer);
  571. }
  572. bool OS_JavaScript::has_touchscreen_ui_hint() const {
  573. return false; //???
  574. }
  575. void OS_JavaScript::main_loop_request_quit() {
  576. if (main_loop)
  577. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  578. }
  579. void OS_JavaScript::reload_gfx() {
  580. if (gfx_init_func)
  581. gfx_init_func(gfx_init_ud, use_gl2, video_mode.width, video_mode.height, video_mode.fullscreen);
  582. if (rasterizer)
  583. rasterizer->reload_vram();
  584. }
  585. Error OS_JavaScript::shell_open(String p_uri) {
  586. /* clang-format off */
  587. EM_ASM_({
  588. window.open(UTF8ToString($0), '_blank');
  589. }, p_uri.utf8().get_data());
  590. /* clang-format on */
  591. return OK;
  592. }
  593. String OS_JavaScript::get_resource_dir() const {
  594. return "/"; //javascript has it's own filesystem for resources inside the APK
  595. }
  596. String OS_JavaScript::get_data_dir() const {
  597. //if (get_data_dir_func)
  598. // return get_data_dir_func();
  599. return "/userfs";
  600. //return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
  601. };
  602. String OS_JavaScript::get_executable_path() const {
  603. return String();
  604. }
  605. void OS_JavaScript::_close_notification_funcs(const String &p_file, int p_flags) {
  606. print_line("close " + p_file + " flags " + itos(p_flags));
  607. if (p_file.begins_with("/userfs") && p_flags & FileAccess::WRITE) {
  608. static_cast<OS_JavaScript *>(get_singleton())->last_sync_time = OS::get_singleton()->get_ticks_msec();
  609. static_cast<OS_JavaScript *>(get_singleton())->time_to_save_sync = 5000; //five seconds since last save
  610. }
  611. }
  612. void OS_JavaScript::process_joysticks() {
  613. int joy_count = emscripten_get_num_gamepads();
  614. for (int i = 0; i < joy_count; i++) {
  615. EmscriptenGamepadEvent state;
  616. emscripten_get_gamepad_status(i, &state);
  617. if (state.connected) {
  618. int num_buttons = MIN(state.numButtons, 18);
  619. int num_axes = MIN(state.numAxes, 8);
  620. for (int j = 0; j < num_buttons; j++) {
  621. float value = state.analogButton[j];
  622. if (String(state.mapping) == "standard" && (j == 6 || j == 7)) {
  623. InputDefault::JoyAxis jx;
  624. jx.min = 0;
  625. jx.value = value;
  626. last_id = input->joy_axis(last_id, i, j, jx);
  627. } else {
  628. last_id = input->joy_button(last_id, i, j, value);
  629. }
  630. }
  631. for (int j = 0; j < num_axes; j++) {
  632. InputDefault::JoyAxis jx;
  633. jx.min = -1;
  634. jx.value = state.axis[j];
  635. last_id = input->joy_axis(last_id, i, j, jx);
  636. }
  637. }
  638. }
  639. }
  640. bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event) {
  641. if (p_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) {
  642. String guid = "";
  643. if (String(p_event->mapping) == "standard")
  644. guid = "Default HTML5 Gamepad";
  645. input->joy_connection_changed(p_event->index, true, String(p_event->id), guid);
  646. } else {
  647. input->joy_connection_changed(p_event->index, false, "");
  648. }
  649. return true;
  650. }
  651. bool OS_JavaScript::is_joy_known(int p_device) {
  652. return input->is_joy_mapped(p_device);
  653. }
  654. String OS_JavaScript::get_joy_guid(int p_device) const {
  655. return input->get_joy_guid_remapped(p_device);
  656. }
  657. OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) {
  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. rasterizer = NULL;
  665. window_maximized = false;
  666. get_data_dir_func = p_get_data_dir_func;
  667. FileAccessUnix::close_notification_func = _close_notification_funcs;
  668. time_to_save_sync = -1;
  669. }
  670. OS_JavaScript::~OS_JavaScript() {
  671. }