os_javascript.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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-2015 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. int OS_JavaScript::get_video_driver_count() const {
  39. return 1;
  40. }
  41. const char * OS_JavaScript::get_video_driver_name(int p_driver) const {
  42. return "GLES2";
  43. }
  44. OS::VideoMode OS_JavaScript::get_default_video_mode() const {
  45. return OS::VideoMode();
  46. }
  47. int OS_JavaScript::get_audio_driver_count() const {
  48. return 1;
  49. }
  50. const char * OS_JavaScript::get_audio_driver_name(int p_driver) const {
  51. return "JavaScript";
  52. }
  53. void OS_JavaScript::initialize_core() {
  54. OS_Unix::initialize_core();
  55. FileAccess::make_default<FileAccessBufferedFA<FileAccessUnix> >(FileAccess::ACCESS_RESOURCES);
  56. }
  57. void OS_JavaScript::set_opengl_extensions(const char* p_gl_extensions) {
  58. ERR_FAIL_COND(!p_gl_extensions);
  59. gl_extensions=p_gl_extensions;
  60. }
  61. void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
  62. print_line("Init OS");
  63. if (gfx_init_func)
  64. gfx_init_func(gfx_init_ud,use_gl2,p_desired.width,p_desired.height,p_desired.fullscreen);
  65. default_videomode=p_desired;
  66. print_line("Init Audio");
  67. AudioDriverManagerSW::add_driver(&audio_driver_javascript);
  68. if (true) {
  69. RasterizerGLES2 *rasterizer_gles22=memnew( RasterizerGLES2(false,false,false,false) );;
  70. rasterizer_gles22->set_use_framebuffers(false); //not supported by emscripten
  71. if (gl_extensions)
  72. rasterizer_gles22->set_extensions(gl_extensions);
  73. rasterizer = rasterizer_gles22;
  74. } else {
  75. // rasterizer = memnew( RasterizerGLES1(true, false) );
  76. }
  77. print_line("Init VS");
  78. visual_server = memnew( VisualServerRaster(rasterizer) );
  79. visual_server->init();
  80. visual_server->cursor_set_visible(false, 0);
  81. /*AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
  82. if (AudioDriverManagerSW::get_driver(p_audio_driver)->init()!=OK) {
  83. ERR_PRINT("Initializing audio failed.");
  84. }*/
  85. print_line("Init SM");
  86. //sample_manager = memnew( SampleManagerMallocSW );
  87. audio_server = memnew( AudioServerJavascript );
  88. print_line("Init Mixer");
  89. //audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
  90. audio_server->init();
  91. print_line("Init SoundServer");
  92. spatial_sound_server = memnew( SpatialSoundServerSW );
  93. spatial_sound_server->init();
  94. print_line("Init SpatialSoundServer");
  95. spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
  96. spatial_sound_2d_server->init();
  97. //
  98. print_line("Init Physicsserver");
  99. physics_server = memnew( PhysicsServerSW );
  100. physics_server->init();
  101. physics_2d_server = memnew( Physics2DServerSW );
  102. physics_2d_server->init();
  103. input = memnew( InputDefault );
  104. }
  105. void OS_JavaScript::set_main_loop( MainLoop * p_main_loop ) {
  106. main_loop=p_main_loop;
  107. input->set_main_loop(p_main_loop);
  108. }
  109. void OS_JavaScript::delete_main_loop() {
  110. memdelete( main_loop );
  111. }
  112. void OS_JavaScript::finalize() {
  113. memdelete(input);
  114. }
  115. void OS_JavaScript::vprint(const char* p_format, va_list p_list, bool p_stderr) {
  116. if (p_stderr) {
  117. vfprintf(stderr,p_format,p_list);
  118. fflush(stderr);
  119. } else {
  120. vprintf(p_format,p_list);
  121. fflush(stdout);
  122. }
  123. }
  124. void OS_JavaScript::print(const char *p_format, ... ) {
  125. va_list argp;
  126. va_start(argp, p_format);
  127. vprintf(p_format, argp );
  128. va_end(argp);
  129. }
  130. void OS_JavaScript::alert(const String& p_alert) {
  131. print("ALERT: %s\n",p_alert.utf8().get_data());
  132. }
  133. void OS_JavaScript::set_mouse_show(bool p_show) {
  134. //javascript has no mouse...
  135. }
  136. void OS_JavaScript::set_mouse_grab(bool p_grab) {
  137. //it really has no mouse...!
  138. }
  139. bool OS_JavaScript::is_mouse_grab_enabled() const {
  140. //*sigh* technology has evolved so much since i was a kid..
  141. return false;
  142. }
  143. Point2 OS_JavaScript::get_mouse_pos() const {
  144. return Point2();
  145. }
  146. int OS_JavaScript::get_mouse_button_state() const {
  147. return 0;
  148. }
  149. void OS_JavaScript::set_window_title(const String& p_title) {
  150. }
  151. //interesting byt not yet
  152. //void set_clipboard(const String& p_text);
  153. //String get_clipboard() const;
  154. void OS_JavaScript::set_video_mode(const VideoMode& p_video_mode,int p_screen) {
  155. }
  156. OS::VideoMode OS_JavaScript::get_video_mode(int p_screen) const {
  157. return default_videomode;
  158. }
  159. Size2 OS_JavaScript::get_window_size() const {
  160. return Vector2(default_videomode.width,default_videomode.height);
  161. }
  162. void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
  163. p_list->push_back(default_videomode);
  164. }
  165. String OS_JavaScript::get_name() {
  166. return "HTML5";
  167. }
  168. MainLoop *OS_JavaScript::get_main_loop() const {
  169. return main_loop;
  170. }
  171. bool OS_JavaScript::can_draw() const {
  172. return true; //always?
  173. }
  174. void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
  175. //javascript really really really has no mouse.. how amazing..
  176. }
  177. void OS_JavaScript::main_loop_begin() {
  178. if (main_loop)
  179. main_loop->init();
  180. }
  181. bool OS_JavaScript::main_loop_iterate() {
  182. if (!main_loop)
  183. return false;
  184. if (time_to_save_sync>=0) {
  185. int64_t newtime = get_ticks_msec();
  186. int64_t elapsed = newtime - last_sync_time;
  187. last_sync_time=newtime;
  188. time_to_save_sync-=elapsed;
  189. print_line("elapsed "+itos(elapsed)+" tts "+itos(time_to_save_sync));
  190. if (time_to_save_sync<0) {
  191. //time to sync, for real
  192. // run 'success'
  193. print_line("DOING SYNCH!");
  194. EM_ASM(
  195. FS.syncfs(function (err) {
  196. assert(!err);
  197. console.log("Synched!");
  198. //ccall('success', 'v');
  199. });
  200. );
  201. }
  202. }
  203. return Main::iteration();
  204. }
  205. void OS_JavaScript::main_loop_end() {
  206. if (main_loop)
  207. main_loop->finish();
  208. }
  209. void OS_JavaScript::main_loop_focusout() {
  210. if (main_loop)
  211. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  212. //audio_driver_javascript.set_pause(true);
  213. }
  214. void OS_JavaScript::main_loop_focusin(){
  215. if (main_loop)
  216. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  217. //audio_driver_javascript.set_pause(false);
  218. }
  219. void OS_JavaScript::push_input(const InputEvent& p_ev) {
  220. InputEvent ev = p_ev;
  221. ev.ID=last_id++;
  222. input->parse_input_event(p_ev);
  223. }
  224. void OS_JavaScript::process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points) {
  225. // print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
  226. switch(p_what) {
  227. case 0: { //gesture begin
  228. if (touch.size()) {
  229. //end all if exist
  230. InputEvent ev;
  231. ev.type=InputEvent::MOUSE_BUTTON;
  232. ev.ID=last_id++;
  233. ev.mouse_button.button_index=BUTTON_LEFT;
  234. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  235. ev.mouse_button.pressed=false;
  236. ev.mouse_button.x=touch[0].pos.x;
  237. ev.mouse_button.y=touch[0].pos.y;
  238. ev.mouse_button.global_x=touch[0].pos.x;
  239. ev.mouse_button.global_y=touch[0].pos.y;
  240. input->parse_input_event(ev);
  241. for(int i=0;i<touch.size();i++) {
  242. InputEvent ev;
  243. ev.type=InputEvent::SCREEN_TOUCH;
  244. ev.ID=last_id++;
  245. ev.screen_touch.index=touch[i].id;
  246. ev.screen_touch.pressed=false;
  247. ev.screen_touch.x=touch[i].pos.x;
  248. ev.screen_touch.y=touch[i].pos.y;
  249. input->parse_input_event(ev);
  250. }
  251. }
  252. touch.resize(p_points.size());
  253. for(int i=0;i<p_points.size();i++) {
  254. touch[i].id=p_points[i].id;
  255. touch[i].pos=p_points[i].pos;
  256. }
  257. {
  258. //send mouse
  259. InputEvent ev;
  260. ev.type=InputEvent::MOUSE_BUTTON;
  261. ev.ID=last_id++;
  262. ev.mouse_button.button_index=BUTTON_LEFT;
  263. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  264. ev.mouse_button.pressed=true;
  265. ev.mouse_button.x=touch[0].pos.x;
  266. ev.mouse_button.y=touch[0].pos.y;
  267. ev.mouse_button.global_x=touch[0].pos.x;
  268. ev.mouse_button.global_y=touch[0].pos.y;
  269. last_mouse=touch[0].pos;
  270. input->parse_input_event(ev);
  271. }
  272. //send touch
  273. for(int i=0;i<touch.size();i++) {
  274. InputEvent ev;
  275. ev.type=InputEvent::SCREEN_TOUCH;
  276. ev.ID=last_id++;
  277. ev.screen_touch.index=touch[i].id;
  278. ev.screen_touch.pressed=true;
  279. ev.screen_touch.x=touch[i].pos.x;
  280. ev.screen_touch.y=touch[i].pos.y;
  281. input->parse_input_event(ev);
  282. }
  283. } break;
  284. case 1: { //motion
  285. if (p_points.size()) {
  286. //send mouse, should look for point 0?
  287. InputEvent ev;
  288. ev.type=InputEvent::MOUSE_MOTION;
  289. ev.ID=last_id++;
  290. ev.mouse_motion.button_mask=BUTTON_MASK_LEFT;
  291. ev.mouse_motion.x=p_points[0].pos.x;
  292. ev.mouse_motion.y=p_points[0].pos.y;
  293. input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
  294. ev.mouse_motion.speed_x=input->get_mouse_speed().x;
  295. ev.mouse_motion.speed_y=input->get_mouse_speed().y;
  296. ev.mouse_motion.relative_x=p_points[0].pos.x-last_mouse.x;
  297. ev.mouse_motion.relative_y=p_points[0].pos.y-last_mouse.y;
  298. last_mouse=p_points[0].pos;
  299. input->parse_input_event(ev);
  300. }
  301. ERR_FAIL_COND(touch.size()!=p_points.size());
  302. for(int i=0;i<touch.size();i++) {
  303. int idx=-1;
  304. for(int j=0;j<p_points.size();j++) {
  305. if (touch[i].id==p_points[j].id) {
  306. idx=j;
  307. break;
  308. }
  309. }
  310. ERR_CONTINUE(idx==-1);
  311. if (touch[i].pos==p_points[idx].pos)
  312. continue; //no move unncesearily
  313. InputEvent ev;
  314. ev.type=InputEvent::SCREEN_DRAG;
  315. ev.ID=last_id++;
  316. ev.screen_drag.index=touch[i].id;
  317. ev.screen_drag.x=p_points[idx].pos.x;
  318. ev.screen_drag.y=p_points[idx].pos.y;
  319. ev.screen_drag.relative_x=p_points[idx].pos.x - touch[i].pos.x;
  320. ev.screen_drag.relative_y=p_points[idx].pos.y - touch[i].pos.y;
  321. input->parse_input_event(ev);
  322. touch[i].pos=p_points[idx].pos;
  323. }
  324. } break;
  325. case 2: { //release
  326. if (touch.size()) {
  327. //end all if exist
  328. InputEvent ev;
  329. ev.type=InputEvent::MOUSE_BUTTON;
  330. ev.ID=last_id++;
  331. ev.mouse_button.button_index=BUTTON_LEFT;
  332. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  333. ev.mouse_button.pressed=false;
  334. ev.mouse_button.x=touch[0].pos.x;
  335. ev.mouse_button.y=touch[0].pos.y;
  336. ev.mouse_button.global_x=touch[0].pos.x;
  337. ev.mouse_button.global_y=touch[0].pos.y;
  338. input->parse_input_event(ev);
  339. for(int i=0;i<touch.size();i++) {
  340. InputEvent ev;
  341. ev.type=InputEvent::SCREEN_TOUCH;
  342. ev.ID=last_id++;
  343. ev.screen_touch.index=touch[i].id;
  344. ev.screen_touch.pressed=false;
  345. ev.screen_touch.x=touch[i].pos.x;
  346. ev.screen_touch.y=touch[i].pos.y;
  347. input->parse_input_event(ev);
  348. }
  349. touch.clear();
  350. }
  351. } break;
  352. case 3: { // add tuchi
  353. ERR_FAIL_INDEX(p_pointer,p_points.size());
  354. TouchPos tp=p_points[p_pointer];
  355. touch.push_back(tp);
  356. InputEvent ev;
  357. ev.type=InputEvent::SCREEN_TOUCH;
  358. ev.ID=last_id++;
  359. ev.screen_touch.index=tp.id;
  360. ev.screen_touch.pressed=true;
  361. ev.screen_touch.x=tp.pos.x;
  362. ev.screen_touch.y=tp.pos.y;
  363. input->parse_input_event(ev);
  364. } break;
  365. case 4: {
  366. for(int i=0;i<touch.size();i++) {
  367. if (touch[i].id==p_pointer) {
  368. InputEvent ev;
  369. ev.type=InputEvent::SCREEN_TOUCH;
  370. ev.ID=last_id++;
  371. ev.screen_touch.index=touch[i].id;
  372. ev.screen_touch.pressed=false;
  373. ev.screen_touch.x=touch[i].pos.x;
  374. ev.screen_touch.y=touch[i].pos.y;
  375. input->parse_input_event(ev);
  376. touch.remove(i);
  377. i--;
  378. }
  379. }
  380. } break;
  381. }
  382. }
  383. void OS_JavaScript::process_accelerometer(const Vector3& p_accelerometer) {
  384. input->set_accelerometer(p_accelerometer);
  385. }
  386. bool OS_JavaScript::has_touchscreen_ui_hint() const {
  387. return false; //???
  388. }
  389. void OS_JavaScript::main_loop_request_quit() {
  390. if (main_loop)
  391. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  392. }
  393. void OS_JavaScript::set_display_size(Size2 p_size) {
  394. default_videomode.width=p_size.x;
  395. default_videomode.height=p_size.y;
  396. }
  397. void OS_JavaScript::reload_gfx() {
  398. if (gfx_init_func)
  399. gfx_init_func(gfx_init_ud,use_gl2,default_videomode.width,default_videomode.height,default_videomode.fullscreen);
  400. if (rasterizer)
  401. rasterizer->reload_vram();
  402. }
  403. Error OS_JavaScript::shell_open(String p_uri) {
  404. if (open_uri_func)
  405. return open_uri_func(p_uri)?ERR_CANT_OPEN:OK;
  406. return ERR_UNAVAILABLE;
  407. };
  408. String OS_JavaScript::get_resource_dir() const {
  409. return "/"; //javascript has it's own filesystem for resources inside the APK
  410. }
  411. String OS_JavaScript::get_locale() const {
  412. if (get_locale_func)
  413. return get_locale_func();
  414. return OS_Unix::get_locale();
  415. }
  416. String OS_JavaScript::get_data_dir() const {
  417. //if (get_data_dir_func)
  418. // return get_data_dir_func();
  419. return "/userfs";
  420. //return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
  421. };
  422. void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) {
  423. print_line("close "+p_file+" flags "+itos(p_flags));
  424. if (p_file.begins_with("/userfs") && p_flags&FileAccess::WRITE) {
  425. static_cast<OS_JavaScript*>(get_singleton())->last_sync_time=OS::get_singleton()->get_ticks_msec();
  426. static_cast<OS_JavaScript*>(get_singleton())->time_to_save_sync=5000; //five seconds since last save
  427. }
  428. }
  429. 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) {
  430. default_videomode.width=800;
  431. default_videomode.height=600;
  432. default_videomode.fullscreen=true;
  433. default_videomode.resizable=false;
  434. gfx_init_func=p_gfx_init_func;
  435. gfx_init_ud=p_gfx_init_ud;
  436. main_loop=NULL;
  437. last_id=1;
  438. gl_extensions=NULL;
  439. rasterizer=NULL;
  440. open_uri_func=p_open_uri_func;
  441. get_data_dir_func=p_get_data_dir_func;
  442. get_locale_func=p_get_locale_func;
  443. FileAccessUnix::close_notification_func=_close_notification_funcs;
  444. time_to_save_sync=-1;
  445. }
  446. OS_JavaScript::~OS_JavaScript() {
  447. }