os_javascript.cpp 20 KB

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