os_javascript.cpp 22 KB

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