os_javascript.cpp 20 KB

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