os_javascript.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. }
  198. void OS_JavaScript::set_main_loop( MainLoop * p_main_loop ) {
  199. main_loop=p_main_loop;
  200. input->set_main_loop(p_main_loop);
  201. }
  202. void OS_JavaScript::delete_main_loop() {
  203. memdelete( main_loop );
  204. }
  205. void OS_JavaScript::finalize() {
  206. memdelete(input);
  207. }
  208. void OS_JavaScript::alert(const String& p_alert,const String& p_title) {
  209. EM_ASM_({
  210. window.alert(UTF8ToString($0));
  211. }, p_alert.utf8().get_data());
  212. }
  213. void OS_JavaScript::set_mouse_show(bool p_show) {
  214. //javascript has no mouse...
  215. }
  216. void OS_JavaScript::set_mouse_grab(bool p_grab) {
  217. //it really has no mouse...!
  218. }
  219. bool OS_JavaScript::is_mouse_grab_enabled() const {
  220. //*sigh* technology has evolved so much since i was a kid..
  221. return false;
  222. }
  223. Point2 OS_JavaScript::get_mouse_pos() const {
  224. return input->get_mouse_pos();
  225. }
  226. int OS_JavaScript::get_mouse_button_state() const {
  227. return last_button_mask;
  228. }
  229. void OS_JavaScript::set_window_title(const String& p_title) {
  230. EM_ASM_({
  231. document.title = UTF8ToString($0);
  232. }, p_title.utf8().get_data());
  233. }
  234. //interesting byt not yet
  235. //void set_clipboard(const String& p_text);
  236. //String get_clipboard() const;
  237. void OS_JavaScript::set_video_mode(const VideoMode& p_video_mode,int p_screen) {
  238. }
  239. OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
  240. return default_videomode;
  241. }
  242. Size2 OS_JavaScript::get_window_size() const {
  243. return Vector2(default_videomode.width,default_videomode.height);
  244. }
  245. void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
  246. p_list->push_back(default_videomode);
  247. }
  248. String OS_JavaScript::get_name() {
  249. return "HTML5";
  250. }
  251. MainLoop *OS_JavaScript::get_main_loop() const {
  252. return main_loop;
  253. }
  254. bool OS_JavaScript::can_draw() const {
  255. return true; //always?
  256. }
  257. void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
  258. //javascript really really really has no mouse.. how amazing..
  259. }
  260. void OS_JavaScript::main_loop_begin() {
  261. if (main_loop)
  262. main_loop->init();
  263. }
  264. bool OS_JavaScript::main_loop_iterate() {
  265. if (!main_loop)
  266. return false;
  267. if (time_to_save_sync>=0) {
  268. int64_t newtime = get_ticks_msec();
  269. int64_t elapsed = newtime - last_sync_time;
  270. last_sync_time=newtime;
  271. time_to_save_sync-=elapsed;
  272. print_line("elapsed "+itos(elapsed)+" tts "+itos(time_to_save_sync));
  273. if (time_to_save_sync<0) {
  274. //time to sync, for real
  275. // run 'success'
  276. print_line("DOING SYNCH!");
  277. EM_ASM(
  278. FS.syncfs(function (err) {
  279. assert(!err);
  280. console.log("Synched!");
  281. //ccall('success', 'v');
  282. });
  283. );
  284. }
  285. }
  286. process_joysticks();
  287. return Main::iteration();
  288. }
  289. void OS_JavaScript::main_loop_end() {
  290. if (main_loop)
  291. main_loop->finish();
  292. }
  293. void OS_JavaScript::main_loop_focusout() {
  294. if (main_loop)
  295. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  296. //audio_driver_javascript.set_pause(true);
  297. }
  298. void OS_JavaScript::main_loop_focusin(){
  299. if (main_loop)
  300. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  301. //audio_driver_javascript.set_pause(false);
  302. }
  303. void OS_JavaScript::push_input(const InputEvent& p_ev) {
  304. InputEvent ev = p_ev;
  305. ev.ID=last_id++;
  306. if (ev.type==InputEvent::MOUSE_MOTION) {
  307. input->set_mouse_pos(Point2(ev.mouse_motion.x, ev.mouse_motion.y));
  308. }
  309. else if (ev.type==InputEvent::MOUSE_BUTTON) {
  310. last_button_mask = ev.mouse_button.button_mask;
  311. }
  312. input->parse_input_event(p_ev);
  313. }
  314. void OS_JavaScript::process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points) {
  315. // print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
  316. switch(p_what) {
  317. case 0: { //gesture begin
  318. if (touch.size()) {
  319. //end all if exist
  320. InputEvent ev;
  321. ev.type=InputEvent::MOUSE_BUTTON;
  322. ev.ID=last_id++;
  323. ev.mouse_button.button_index=BUTTON_LEFT;
  324. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  325. ev.mouse_button.pressed=false;
  326. ev.mouse_button.x=touch[0].pos.x;
  327. ev.mouse_button.y=touch[0].pos.y;
  328. ev.mouse_button.global_x=touch[0].pos.x;
  329. ev.mouse_button.global_y=touch[0].pos.y;
  330. input->parse_input_event(ev);
  331. for(int i=0;i<touch.size();i++) {
  332. InputEvent ev;
  333. ev.type=InputEvent::SCREEN_TOUCH;
  334. ev.ID=last_id++;
  335. ev.screen_touch.index=touch[i].id;
  336. ev.screen_touch.pressed=false;
  337. ev.screen_touch.x=touch[i].pos.x;
  338. ev.screen_touch.y=touch[i].pos.y;
  339. input->parse_input_event(ev);
  340. }
  341. }
  342. touch.resize(p_points.size());
  343. for(int i=0;i<p_points.size();i++) {
  344. touch[i].id=p_points[i].id;
  345. touch[i].pos=p_points[i].pos;
  346. }
  347. {
  348. //send mouse
  349. InputEvent ev;
  350. ev.type=InputEvent::MOUSE_BUTTON;
  351. ev.ID=last_id++;
  352. ev.mouse_button.button_index=BUTTON_LEFT;
  353. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  354. ev.mouse_button.pressed=true;
  355. ev.mouse_button.x=touch[0].pos.x;
  356. ev.mouse_button.y=touch[0].pos.y;
  357. ev.mouse_button.global_x=touch[0].pos.x;
  358. ev.mouse_button.global_y=touch[0].pos.y;
  359. last_mouse=touch[0].pos;
  360. input->parse_input_event(ev);
  361. }
  362. //send touch
  363. for(int i=0;i<touch.size();i++) {
  364. InputEvent ev;
  365. ev.type=InputEvent::SCREEN_TOUCH;
  366. ev.ID=last_id++;
  367. ev.screen_touch.index=touch[i].id;
  368. ev.screen_touch.pressed=true;
  369. ev.screen_touch.x=touch[i].pos.x;
  370. ev.screen_touch.y=touch[i].pos.y;
  371. input->parse_input_event(ev);
  372. }
  373. } break;
  374. case 1: { //motion
  375. if (p_points.size()) {
  376. //send mouse, should look for point 0?
  377. InputEvent ev;
  378. ev.type=InputEvent::MOUSE_MOTION;
  379. ev.ID=last_id++;
  380. ev.mouse_motion.button_mask=BUTTON_MASK_LEFT;
  381. ev.mouse_motion.x=p_points[0].pos.x;
  382. ev.mouse_motion.y=p_points[0].pos.y;
  383. input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
  384. ev.mouse_motion.speed_x=input->get_mouse_speed().x;
  385. ev.mouse_motion.speed_y=input->get_mouse_speed().y;
  386. ev.mouse_motion.relative_x=p_points[0].pos.x-last_mouse.x;
  387. ev.mouse_motion.relative_y=p_points[0].pos.y-last_mouse.y;
  388. last_mouse=p_points[0].pos;
  389. input->parse_input_event(ev);
  390. }
  391. ERR_FAIL_COND(touch.size()!=p_points.size());
  392. for(int i=0;i<touch.size();i++) {
  393. int idx=-1;
  394. for(int j=0;j<p_points.size();j++) {
  395. if (touch[i].id==p_points[j].id) {
  396. idx=j;
  397. break;
  398. }
  399. }
  400. ERR_CONTINUE(idx==-1);
  401. if (touch[i].pos==p_points[idx].pos)
  402. continue; //no move unncesearily
  403. InputEvent ev;
  404. ev.type=InputEvent::SCREEN_DRAG;
  405. ev.ID=last_id++;
  406. ev.screen_drag.index=touch[i].id;
  407. ev.screen_drag.x=p_points[idx].pos.x;
  408. ev.screen_drag.y=p_points[idx].pos.y;
  409. ev.screen_drag.relative_x=p_points[idx].pos.x - touch[i].pos.x;
  410. ev.screen_drag.relative_y=p_points[idx].pos.y - touch[i].pos.y;
  411. input->parse_input_event(ev);
  412. touch[i].pos=p_points[idx].pos;
  413. }
  414. } break;
  415. case 2: { //release
  416. if (touch.size()) {
  417. //end all if exist
  418. InputEvent ev;
  419. ev.type=InputEvent::MOUSE_BUTTON;
  420. ev.ID=last_id++;
  421. ev.mouse_button.button_index=BUTTON_LEFT;
  422. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  423. ev.mouse_button.pressed=false;
  424. ev.mouse_button.x=touch[0].pos.x;
  425. ev.mouse_button.y=touch[0].pos.y;
  426. ev.mouse_button.global_x=touch[0].pos.x;
  427. ev.mouse_button.global_y=touch[0].pos.y;
  428. input->parse_input_event(ev);
  429. for(int i=0;i<touch.size();i++) {
  430. InputEvent ev;
  431. ev.type=InputEvent::SCREEN_TOUCH;
  432. ev.ID=last_id++;
  433. ev.screen_touch.index=touch[i].id;
  434. ev.screen_touch.pressed=false;
  435. ev.screen_touch.x=touch[i].pos.x;
  436. ev.screen_touch.y=touch[i].pos.y;
  437. input->parse_input_event(ev);
  438. }
  439. touch.clear();
  440. }
  441. } break;
  442. case 3: { // add tuchi
  443. ERR_FAIL_INDEX(p_pointer,p_points.size());
  444. TouchPos tp=p_points[p_pointer];
  445. touch.push_back(tp);
  446. InputEvent ev;
  447. ev.type=InputEvent::SCREEN_TOUCH;
  448. ev.ID=last_id++;
  449. ev.screen_touch.index=tp.id;
  450. ev.screen_touch.pressed=true;
  451. ev.screen_touch.x=tp.pos.x;
  452. ev.screen_touch.y=tp.pos.y;
  453. input->parse_input_event(ev);
  454. } break;
  455. case 4: {
  456. for(int i=0;i<touch.size();i++) {
  457. if (touch[i].id==p_pointer) {
  458. InputEvent ev;
  459. ev.type=InputEvent::SCREEN_TOUCH;
  460. ev.ID=last_id++;
  461. ev.screen_touch.index=touch[i].id;
  462. ev.screen_touch.pressed=false;
  463. ev.screen_touch.x=touch[i].pos.x;
  464. ev.screen_touch.y=touch[i].pos.y;
  465. input->parse_input_event(ev);
  466. touch.remove(i);
  467. i--;
  468. }
  469. }
  470. } break;
  471. }
  472. }
  473. void OS_JavaScript::process_accelerometer(const Vector3& p_accelerometer) {
  474. input->set_accelerometer(p_accelerometer);
  475. }
  476. bool OS_JavaScript::has_touchscreen_ui_hint() const {
  477. return false; //???
  478. }
  479. void OS_JavaScript::main_loop_request_quit() {
  480. if (main_loop)
  481. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  482. }
  483. void OS_JavaScript::set_display_size(Size2 p_size) {
  484. default_videomode.width=p_size.x;
  485. default_videomode.height=p_size.y;
  486. }
  487. void OS_JavaScript::reload_gfx() {
  488. if (gfx_init_func)
  489. gfx_init_func(gfx_init_ud,use_gl2,default_videomode.width,default_videomode.height,default_videomode.fullscreen);
  490. if (rasterizer)
  491. rasterizer->reload_vram();
  492. }
  493. Error OS_JavaScript::shell_open(String p_uri) {
  494. EM_ASM_({
  495. window.open(UTF8ToString($0), '_blank');
  496. }, p_uri.utf8().get_data());
  497. return OK;
  498. }
  499. String OS_JavaScript::get_resource_dir() const {
  500. return "/"; //javascript has it's own filesystem for resources inside the APK
  501. }
  502. String OS_JavaScript::get_data_dir() const {
  503. //if (get_data_dir_func)
  504. // return get_data_dir_func();
  505. return "/userfs";
  506. //return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
  507. };
  508. String OS_JavaScript::get_executable_path() const {
  509. return String();
  510. }
  511. void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) {
  512. print_line("close "+p_file+" flags "+itos(p_flags));
  513. if (p_file.begins_with("/userfs") && p_flags&FileAccess::WRITE) {
  514. static_cast<OS_JavaScript*>(get_singleton())->last_sync_time=OS::get_singleton()->get_ticks_msec();
  515. static_cast<OS_JavaScript*>(get_singleton())->time_to_save_sync=5000; //five seconds since last save
  516. }
  517. }
  518. void OS_JavaScript::process_joysticks() {
  519. int joy_count = emscripten_get_num_gamepads();
  520. for (int i = 0; i < joy_count; i++) {
  521. EmscriptenGamepadEvent state;
  522. emscripten_get_gamepad_status(i, &state);
  523. if (state.connected) {
  524. int num_buttons = MIN(state.numButtons, 18);
  525. int num_axes = MIN(state.numAxes, 8);
  526. for (int j = 0; j < num_buttons; j++) {
  527. float value = state.analogButton[j];
  528. if (String(state.mapping) == "standard" && (j == 6 || j == 7)) {
  529. InputDefault::JoyAxis jx;
  530. jx.min = 0;
  531. jx.value = value;
  532. last_id = input->joy_axis(last_id, i, j, jx);
  533. }
  534. else {
  535. last_id = input->joy_button(last_id, i, j, value);
  536. }
  537. }
  538. for (int j = 0; j < num_axes; j++) {
  539. InputDefault::JoyAxis jx;
  540. jx.min = -1;
  541. jx.value = state.axis[j];
  542. last_id = input->joy_axis(last_id, i, j, jx);
  543. }
  544. }
  545. }
  546. }
  547. bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event) {
  548. if (p_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) {
  549. String guid = "";
  550. if (String(p_event->mapping) == "standard")
  551. guid = "Default HTML5 Gamepad";
  552. input->joy_connection_changed(p_event->index, true, String(p_event->id), guid);
  553. }
  554. else {
  555. input->joy_connection_changed(p_event->index, false, "");
  556. }
  557. return true;
  558. }
  559. bool OS_JavaScript::is_joy_known(int p_device) {
  560. return input->is_joy_mapped(p_device);
  561. }
  562. String OS_JavaScript::get_joy_guid(int p_device) const {
  563. return input->get_joy_guid_remapped(p_device);
  564. }
  565. OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) {
  566. default_videomode.width=800;
  567. default_videomode.height=600;
  568. default_videomode.fullscreen=true;
  569. default_videomode.resizable=false;
  570. gfx_init_func=p_gfx_init_func;
  571. gfx_init_ud=p_gfx_init_ud;
  572. last_button_mask=0;
  573. main_loop=NULL;
  574. last_id=1;
  575. gl_extensions=NULL;
  576. rasterizer=NULL;
  577. get_data_dir_func=p_get_data_dir_func;
  578. FileAccessUnix::close_notification_func=_close_notification_funcs;
  579. time_to_save_sync=-1;
  580. }
  581. OS_JavaScript::~OS_JavaScript() {
  582. }