os_javascript.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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-2016 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 "drivers/gles2/rasterizer_gles2.h"
  31. #include "core/io/file_access_buffered_fa.h"
  32. #include "drivers/unix/file_access_unix.h"
  33. #include "drivers/unix/dir_access_unix.h"
  34. #include "servers/visual/visual_server_raster.h"
  35. #include "main/main.h"
  36. #include "core/globals.h"
  37. #include "stdlib.h"
  38. #include "emscripten.h"
  39. #include "dom_keys.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 "GLES2";
  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 InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_event) {
  64. InputEvent ev;
  65. ev.type = InputEvent::KEY;
  66. ev.key.echo = emscripten_event->repeat;
  67. ev.key.mod.alt = emscripten_event->altKey;
  68. ev.key.mod.shift = emscripten_event->shiftKey;
  69. ev.key.mod.control = emscripten_event->ctrlKey;
  70. ev.key.mod.meta = emscripten_event->metaKey;
  71. ev.key.scancode = dom2godot_scancode(emscripten_event->keyCode);
  72. String unicode = String::utf8(emscripten_event->key);
  73. // check if empty or multi-character (e.g. `CapsLock`)
  74. if (unicode.length()!=1) {
  75. // might be empty as well, but better than nonsense
  76. unicode = String::utf8(emscripten_event->charValue);
  77. }
  78. if (unicode.length()==1) {
  79. ev.key.unicode=unicode[0];
  80. }
  81. return ev;
  82. }
  83. static InputEvent deferred_key_event;
  84. static EM_BOOL _keydown_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  85. ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_KEYDOWN, false);
  86. InputEvent ev = _setup_key_event(key_event);
  87. ev.key.pressed = true;
  88. if (ev.key.unicode==0 && keycode_has_unicode(ev.key.scancode)) {
  89. // defer to keypress event for legacy unicode retrieval
  90. deferred_key_event = ev;
  91. return false; // do not suppress keypress event
  92. }
  93. static_cast<OS_JavaScript*>(user_data)->push_input(ev);
  94. return true;
  95. }
  96. static EM_BOOL _keypress_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  97. ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_KEYPRESS, false);
  98. deferred_key_event.key.unicode = key_event->charCode;
  99. static_cast<OS_JavaScript*>(user_data)->push_input(deferred_key_event);
  100. return true;
  101. }
  102. static EM_BOOL _keyup_callback(int event_type, const EmscriptenKeyboardEvent *key_event, void *user_data) {
  103. ERR_FAIL_COND_V(event_type!=EMSCRIPTEN_EVENT_KEYUP, false);
  104. InputEvent ev = _setup_key_event(key_event);
  105. ev.key.pressed = false;
  106. static_cast<OS_JavaScript*>(user_data)->push_input(ev);
  107. return ev.key.scancode!=KEY_UNKNOWN && ev.key.scancode!=0;
  108. }
  109. static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) {
  110. OS_JavaScript *os = (OS_JavaScript*) OS::get_singleton();
  111. if (os) {
  112. return os->joy_connection_changed(p_type, p_event);
  113. }
  114. return false;
  115. }
  116. void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
  117. print_line("Init OS");
  118. if (gfx_init_func)
  119. gfx_init_func(gfx_init_ud,use_gl2,p_desired.width,p_desired.height,p_desired.fullscreen);
  120. default_videomode=p_desired;
  121. // find locale, emscripten only sets "C"
  122. char locale_ptr[16];
  123. EM_ASM_({
  124. var locale = "";
  125. if (Module.locale) {
  126. // best case: server-side script reads Accept-Language early and
  127. // defines the locale to be read here
  128. locale = Module.locale;
  129. } else {
  130. // no luck, use what the JS engine can tell us
  131. // if this turns out not compatible enough, add tests for
  132. // browserLanguage, systemLanguage and userLanguage
  133. locale = navigator.languages ? navigator.languages[0] : navigator.language;
  134. }
  135. locale = locale.split('.')[0];
  136. stringToUTF8(locale, $0, 16);
  137. }, locale_ptr);
  138. setenv("LANG", locale_ptr, true);
  139. print_line("Init Audio");
  140. AudioDriverManagerSW::add_driver(&audio_driver_javascript);
  141. if (true) {
  142. RasterizerGLES2 *rasterizer_gles22=memnew( RasterizerGLES2(false,false,false,false) );;
  143. rasterizer_gles22->set_use_framebuffers(false); //not supported by emscripten
  144. if (gl_extensions)
  145. rasterizer_gles22->set_extensions(gl_extensions);
  146. rasterizer = rasterizer_gles22;
  147. } else {
  148. // rasterizer = memnew( RasterizerGLES1(true, false) );
  149. }
  150. print_line("Init VS");
  151. visual_server = memnew( VisualServerRaster(rasterizer) );
  152. visual_server->init();
  153. visual_server->cursor_set_visible(false, 0);
  154. /*AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
  155. if (AudioDriverManagerSW::get_driver(p_audio_driver)->init()!=OK) {
  156. ERR_PRINT("Initializing audio failed.");
  157. }*/
  158. print_line("Init SM");
  159. //sample_manager = memnew( SampleManagerMallocSW );
  160. audio_server = memnew( AudioServerJavascript );
  161. print_line("Init Mixer");
  162. //audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
  163. audio_server->init();
  164. print_line("Init SoundServer");
  165. spatial_sound_server = memnew( SpatialSoundServerSW );
  166. spatial_sound_server->init();
  167. print_line("Init SpatialSoundServer");
  168. spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
  169. spatial_sound_2d_server->init();
  170. //
  171. print_line("Init Physicsserver");
  172. physics_server = memnew( PhysicsServerSW );
  173. physics_server->init();
  174. physics_2d_server = memnew( Physics2DServerSW );
  175. physics_2d_server->init();
  176. input = memnew( InputDefault );
  177. EMSCRIPTEN_RESULT result = emscripten_set_keydown_callback(NULL, this , true, &_keydown_callback);
  178. if (result!=EMSCRIPTEN_RESULT_SUCCESS) {
  179. ERR_PRINTS( "Error while setting Emscripten keydown callback: Code " + itos(result) );
  180. }
  181. result = emscripten_set_keypress_callback(NULL, this, true, &_keypress_callback);
  182. if (result!=EMSCRIPTEN_RESULT_SUCCESS) {
  183. ERR_PRINTS( "Error while setting Emscripten keypress callback: Code " + itos(result) );
  184. }
  185. result = emscripten_set_keyup_callback(NULL, this, true, &_keyup_callback);
  186. if (result!=EMSCRIPTEN_RESULT_SUCCESS) {
  187. ERR_PRINTS( "Error while setting Emscripten keyup callback: Code " + itos(result) );
  188. }
  189. result = emscripten_set_gamepadconnected_callback(NULL, true, &joy_callback_func);
  190. if (result!=EMSCRIPTEN_RESULT_SUCCESS) {
  191. ERR_PRINTS( "Error while setting Emscripten gamepadconnected callback: Code " + itos(result) );
  192. }
  193. result = emscripten_set_gamepaddisconnected_callback(NULL, true, &joy_callback_func);
  194. if (result!=EMSCRIPTEN_RESULT_SUCCESS) {
  195. ERR_PRINTS( "Error while setting Emscripten gamepaddisconnected callback: Code " + itos(result) );
  196. }
  197. #ifdef JAVASCRIPT_EVAL_ENABLED
  198. javascript_eval = memnew(JavaScript);
  199. Globals::get_singleton()->add_singleton(Globals::Singleton("JavaScript", javascript_eval));
  200. #endif
  201. }
  202. void OS_JavaScript::set_main_loop( MainLoop * p_main_loop ) {
  203. main_loop=p_main_loop;
  204. input->set_main_loop(p_main_loop);
  205. }
  206. void OS_JavaScript::delete_main_loop() {
  207. memdelete( main_loop );
  208. }
  209. void OS_JavaScript::finalize() {
  210. memdelete(input);
  211. }
  212. void OS_JavaScript::alert(const String& p_alert,const String& p_title) {
  213. EM_ASM_({
  214. window.alert(UTF8ToString($0));
  215. }, p_alert.utf8().get_data());
  216. }
  217. void OS_JavaScript::set_mouse_show(bool p_show) {
  218. //javascript has no mouse...
  219. }
  220. void OS_JavaScript::set_mouse_grab(bool p_grab) {
  221. //it really has no mouse...!
  222. }
  223. bool OS_JavaScript::is_mouse_grab_enabled() const {
  224. //*sigh* technology has evolved so much since i was a kid..
  225. return false;
  226. }
  227. Point2 OS_JavaScript::get_mouse_pos() const {
  228. return input->get_mouse_pos();
  229. }
  230. int OS_JavaScript::get_mouse_button_state() const {
  231. return last_button_mask;
  232. }
  233. void OS_JavaScript::set_window_title(const String& p_title) {
  234. EM_ASM_({
  235. document.title = UTF8ToString($0);
  236. }, p_title.utf8().get_data());
  237. }
  238. //interesting byt not yet
  239. //void set_clipboard(const String& p_text);
  240. //String get_clipboard() const;
  241. void OS_JavaScript::set_video_mode(const VideoMode& p_video_mode,int p_screen) {
  242. }
  243. OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
  244. return default_videomode;
  245. }
  246. Size2 OS_JavaScript::get_window_size() const {
  247. return Vector2(default_videomode.width,default_videomode.height);
  248. }
  249. void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
  250. p_list->push_back(default_videomode);
  251. }
  252. String OS_JavaScript::get_name() {
  253. return "HTML5";
  254. }
  255. MainLoop *OS_JavaScript::get_main_loop() const {
  256. return main_loop;
  257. }
  258. bool OS_JavaScript::can_draw() const {
  259. return true; //always?
  260. }
  261. void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
  262. //javascript really really really has no mouse.. how amazing..
  263. }
  264. void OS_JavaScript::main_loop_begin() {
  265. if (main_loop)
  266. main_loop->init();
  267. }
  268. bool OS_JavaScript::main_loop_iterate() {
  269. if (!main_loop)
  270. return false;
  271. if (time_to_save_sync>=0) {
  272. int64_t newtime = get_ticks_msec();
  273. int64_t elapsed = newtime - last_sync_time;
  274. last_sync_time=newtime;
  275. time_to_save_sync-=elapsed;
  276. print_line("elapsed "+itos(elapsed)+" tts "+itos(time_to_save_sync));
  277. if (time_to_save_sync<0) {
  278. //time to sync, for real
  279. // run 'success'
  280. print_line("DOING SYNCH!");
  281. EM_ASM(
  282. FS.syncfs(function (err) {
  283. assert(!err);
  284. console.log("Synched!");
  285. //ccall('success', 'v');
  286. });
  287. );
  288. }
  289. }
  290. process_joysticks();
  291. return Main::iteration();
  292. }
  293. void OS_JavaScript::main_loop_end() {
  294. if (main_loop)
  295. main_loop->finish();
  296. }
  297. void OS_JavaScript::main_loop_focusout() {
  298. if (main_loop)
  299. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  300. //audio_driver_javascript.set_pause(true);
  301. }
  302. void OS_JavaScript::main_loop_focusin(){
  303. if (main_loop)
  304. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  305. //audio_driver_javascript.set_pause(false);
  306. }
  307. void OS_JavaScript::push_input(const InputEvent& p_ev) {
  308. InputEvent ev = p_ev;
  309. ev.ID=last_id++;
  310. if (ev.type==InputEvent::MOUSE_MOTION) {
  311. input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
  312. }
  313. else if (ev.type==InputEvent::MOUSE_BUTTON) {
  314. last_button_mask = ev.mouse_button.button_mask;
  315. }
  316. input->parse_input_event(p_ev);
  317. }
  318. void OS_JavaScript::process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points) {
  319. // print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
  320. switch(p_what) {
  321. case 0: { //gesture begin
  322. if (touch.size()) {
  323. //end all if exist
  324. InputEvent ev;
  325. ev.type=InputEvent::MOUSE_BUTTON;
  326. ev.ID=last_id++;
  327. ev.mouse_button.button_index=BUTTON_LEFT;
  328. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  329. ev.mouse_button.pressed=false;
  330. ev.mouse_button.x=touch[0].pos.x;
  331. ev.mouse_button.y=touch[0].pos.y;
  332. ev.mouse_button.global_x=touch[0].pos.x;
  333. ev.mouse_button.global_y=touch[0].pos.y;
  334. input->parse_input_event(ev);
  335. for(int i=0;i<touch.size();i++) {
  336. InputEvent ev;
  337. ev.type=InputEvent::SCREEN_TOUCH;
  338. ev.ID=last_id++;
  339. ev.screen_touch.index=touch[i].id;
  340. ev.screen_touch.pressed=false;
  341. ev.screen_touch.x=touch[i].pos.x;
  342. ev.screen_touch.y=touch[i].pos.y;
  343. input->parse_input_event(ev);
  344. }
  345. }
  346. touch.resize(p_points.size());
  347. for(int i=0;i<p_points.size();i++) {
  348. touch[i].id=p_points[i].id;
  349. touch[i].pos=p_points[i].pos;
  350. }
  351. {
  352. //send mouse
  353. InputEvent ev;
  354. ev.type=InputEvent::MOUSE_BUTTON;
  355. ev.ID=last_id++;
  356. ev.mouse_button.button_index=BUTTON_LEFT;
  357. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  358. ev.mouse_button.pressed=true;
  359. ev.mouse_button.x=touch[0].pos.x;
  360. ev.mouse_button.y=touch[0].pos.y;
  361. ev.mouse_button.global_x=touch[0].pos.x;
  362. ev.mouse_button.global_y=touch[0].pos.y;
  363. last_mouse=touch[0].pos;
  364. input->parse_input_event(ev);
  365. }
  366. //send touch
  367. for(int i=0;i<touch.size();i++) {
  368. InputEvent ev;
  369. ev.type=InputEvent::SCREEN_TOUCH;
  370. ev.ID=last_id++;
  371. ev.screen_touch.index=touch[i].id;
  372. ev.screen_touch.pressed=true;
  373. ev.screen_touch.x=touch[i].pos.x;
  374. ev.screen_touch.y=touch[i].pos.y;
  375. input->parse_input_event(ev);
  376. }
  377. } break;
  378. case 1: { //motion
  379. if (p_points.size()) {
  380. //send mouse, should look for point 0?
  381. InputEvent ev;
  382. ev.type=InputEvent::MOUSE_MOTION;
  383. ev.ID=last_id++;
  384. ev.mouse_motion.button_mask=BUTTON_MASK_LEFT;
  385. ev.mouse_motion.x=p_points[0].pos.x;
  386. ev.mouse_motion.y=p_points[0].pos.y;
  387. input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
  388. ev.mouse_motion.speed_x=input->get_mouse_speed().x;
  389. ev.mouse_motion.speed_y=input->get_mouse_speed().y;
  390. ev.mouse_motion.relative_x=p_points[0].pos.x-last_mouse.x;
  391. ev.mouse_motion.relative_y=p_points[0].pos.y-last_mouse.y;
  392. last_mouse=p_points[0].pos;
  393. input->parse_input_event(ev);
  394. }
  395. ERR_FAIL_COND(touch.size()!=p_points.size());
  396. for(int i=0;i<touch.size();i++) {
  397. int idx=-1;
  398. for(int j=0;j<p_points.size();j++) {
  399. if (touch[i].id==p_points[j].id) {
  400. idx=j;
  401. break;
  402. }
  403. }
  404. ERR_CONTINUE(idx==-1);
  405. if (touch[i].pos==p_points[idx].pos)
  406. continue; //no move unncesearily
  407. InputEvent ev;
  408. ev.type=InputEvent::SCREEN_DRAG;
  409. ev.ID=last_id++;
  410. ev.screen_drag.index=touch[i].id;
  411. ev.screen_drag.x=p_points[idx].pos.x;
  412. ev.screen_drag.y=p_points[idx].pos.y;
  413. ev.screen_drag.relative_x=p_points[idx].pos.x - touch[i].pos.x;
  414. ev.screen_drag.relative_y=p_points[idx].pos.y - touch[i].pos.y;
  415. input->parse_input_event(ev);
  416. touch[i].pos=p_points[idx].pos;
  417. }
  418. } break;
  419. case 2: { //release
  420. if (touch.size()) {
  421. //end all if exist
  422. InputEvent ev;
  423. ev.type=InputEvent::MOUSE_BUTTON;
  424. ev.ID=last_id++;
  425. ev.mouse_button.button_index=BUTTON_LEFT;
  426. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  427. ev.mouse_button.pressed=false;
  428. ev.mouse_button.x=touch[0].pos.x;
  429. ev.mouse_button.y=touch[0].pos.y;
  430. ev.mouse_button.global_x=touch[0].pos.x;
  431. ev.mouse_button.global_y=touch[0].pos.y;
  432. input->parse_input_event(ev);
  433. for(int i=0;i<touch.size();i++) {
  434. InputEvent ev;
  435. ev.type=InputEvent::SCREEN_TOUCH;
  436. ev.ID=last_id++;
  437. ev.screen_touch.index=touch[i].id;
  438. ev.screen_touch.pressed=false;
  439. ev.screen_touch.x=touch[i].pos.x;
  440. ev.screen_touch.y=touch[i].pos.y;
  441. input->parse_input_event(ev);
  442. }
  443. touch.clear();
  444. }
  445. } break;
  446. case 3: { // add tuchi
  447. ERR_FAIL_INDEX(p_pointer,p_points.size());
  448. TouchPos tp=p_points[p_pointer];
  449. touch.push_back(tp);
  450. InputEvent ev;
  451. ev.type=InputEvent::SCREEN_TOUCH;
  452. ev.ID=last_id++;
  453. ev.screen_touch.index=tp.id;
  454. ev.screen_touch.pressed=true;
  455. ev.screen_touch.x=tp.pos.x;
  456. ev.screen_touch.y=tp.pos.y;
  457. input->parse_input_event(ev);
  458. } break;
  459. case 4: {
  460. for(int i=0;i<touch.size();i++) {
  461. if (touch[i].id==p_pointer) {
  462. InputEvent ev;
  463. ev.type=InputEvent::SCREEN_TOUCH;
  464. ev.ID=last_id++;
  465. ev.screen_touch.index=touch[i].id;
  466. ev.screen_touch.pressed=false;
  467. ev.screen_touch.x=touch[i].pos.x;
  468. ev.screen_touch.y=touch[i].pos.y;
  469. input->parse_input_event(ev);
  470. touch.remove(i);
  471. i--;
  472. }
  473. }
  474. } break;
  475. }
  476. }
  477. void OS_JavaScript::process_accelerometer(const Vector3& p_accelerometer) {
  478. input->set_accelerometer(p_accelerometer);
  479. }
  480. bool OS_JavaScript::has_touchscreen_ui_hint() const {
  481. return false; //???
  482. }
  483. void OS_JavaScript::main_loop_request_quit() {
  484. if (main_loop)
  485. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  486. }
  487. void OS_JavaScript::set_display_size(Size2 p_size) {
  488. default_videomode.width=p_size.x;
  489. default_videomode.height=p_size.y;
  490. }
  491. void OS_JavaScript::reload_gfx() {
  492. if (gfx_init_func)
  493. gfx_init_func(gfx_init_ud,use_gl2,default_videomode.width,default_videomode.height,default_videomode.fullscreen);
  494. if (rasterizer)
  495. rasterizer->reload_vram();
  496. }
  497. Error OS_JavaScript::shell_open(String p_uri) {
  498. EM_ASM_({
  499. window.open(UTF8ToString($0), '_blank');
  500. }, p_uri.utf8().get_data());
  501. return OK;
  502. }
  503. String OS_JavaScript::get_resource_dir() const {
  504. return "/"; //javascript has it's own filesystem for resources inside the APK
  505. }
  506. String OS_JavaScript::get_data_dir() const {
  507. //if (get_data_dir_func)
  508. // return get_data_dir_func();
  509. return "/userfs";
  510. //return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
  511. };
  512. String OS_JavaScript::get_executable_path() const {
  513. return String();
  514. }
  515. void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) {
  516. print_line("close "+p_file+" flags "+itos(p_flags));
  517. if (p_file.begins_with("/userfs") && p_flags&FileAccess::WRITE) {
  518. static_cast<OS_JavaScript*>(get_singleton())->last_sync_time=OS::get_singleton()->get_ticks_msec();
  519. static_cast<OS_JavaScript*>(get_singleton())->time_to_save_sync=5000; //five seconds since last save
  520. }
  521. }
  522. void OS_JavaScript::process_joysticks() {
  523. int joy_count = emscripten_get_num_gamepads();
  524. for (int i = 0; i < joy_count; i++) {
  525. EmscriptenGamepadEvent state;
  526. emscripten_get_gamepad_status(i, &state);
  527. if (state.connected) {
  528. int num_buttons = MIN(state.numButtons, 18);
  529. int num_axes = MIN(state.numAxes, 8);
  530. for (int j = 0; j < num_buttons; j++) {
  531. float value = state.analogButton[j];
  532. if (String(state.mapping) == "standard" && (j == 6 || j == 7)) {
  533. InputDefault::JoyAxis jx;
  534. jx.min = 0;
  535. jx.value = value;
  536. last_id = input->joy_axis(last_id, i, j, jx);
  537. }
  538. else {
  539. last_id = input->joy_button(last_id, i, j, value);
  540. }
  541. }
  542. for (int j = 0; j < num_axes; j++) {
  543. InputDefault::JoyAxis jx;
  544. jx.min = -1;
  545. jx.value = state.axis[j];
  546. last_id = input->joy_axis(last_id, i, j, jx);
  547. }
  548. }
  549. }
  550. }
  551. bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event) {
  552. if (p_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) {
  553. String guid = "";
  554. if (String(p_event->mapping) == "standard")
  555. guid = "Default HTML5 Gamepad";
  556. input->joy_connection_changed(p_event->index, true, String(p_event->id), guid);
  557. }
  558. else {
  559. input->joy_connection_changed(p_event->index, false, "");
  560. }
  561. return true;
  562. }
  563. bool OS_JavaScript::is_joy_known(int p_device) {
  564. return input->is_joy_mapped(p_device);
  565. }
  566. String OS_JavaScript::get_joy_guid(int p_device) const {
  567. return input->get_joy_guid_remapped(p_device);
  568. }
  569. OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) {
  570. default_videomode.width=800;
  571. default_videomode.height=600;
  572. default_videomode.fullscreen=true;
  573. default_videomode.resizable=false;
  574. gfx_init_func=p_gfx_init_func;
  575. gfx_init_ud=p_gfx_init_ud;
  576. last_button_mask=0;
  577. main_loop=NULL;
  578. last_id=1;
  579. gl_extensions=NULL;
  580. rasterizer=NULL;
  581. get_data_dir_func=p_get_data_dir_func;
  582. FileAccessUnix::close_notification_func=_close_notification_funcs;
  583. time_to_save_sync=-1;
  584. }
  585. OS_JavaScript::~OS_JavaScript() {
  586. }