os_javascript.cpp 14 KB

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