os_javascript.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. Size2 OS_JavaScript::get_window_size() const {
  159. return Vector2(default_videomode.width,default_videomode.height);
  160. }
  161. void OS_JavaScript::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
  162. p_list->push_back(default_videomode);
  163. }
  164. String OS_JavaScript::get_name() {
  165. return "HTML5";
  166. }
  167. MainLoop *OS_JavaScript::get_main_loop() const {
  168. return main_loop;
  169. }
  170. bool OS_JavaScript::can_draw() const {
  171. return true; //always?
  172. }
  173. void OS_JavaScript::set_cursor_shape(CursorShape p_shape) {
  174. //javascript really really really has no mouse.. how amazing..
  175. }
  176. void OS_JavaScript::main_loop_begin() {
  177. if (main_loop)
  178. main_loop->init();
  179. }
  180. bool OS_JavaScript::main_loop_iterate() {
  181. if (!main_loop)
  182. return false;
  183. return Main::iteration();
  184. }
  185. void OS_JavaScript::main_loop_end() {
  186. if (main_loop)
  187. main_loop->finish();
  188. }
  189. void OS_JavaScript::main_loop_focusout() {
  190. if (main_loop)
  191. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  192. //audio_driver_javascript.set_pause(true);
  193. }
  194. void OS_JavaScript::main_loop_focusin(){
  195. if (main_loop)
  196. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  197. //audio_driver_javascript.set_pause(false);
  198. }
  199. void OS_JavaScript::push_input(const InputEvent& p_ev) {
  200. InputEvent ev = p_ev;
  201. ev.ID=last_id++;
  202. input->parse_input_event(p_ev);
  203. }
  204. void OS_JavaScript::process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points) {
  205. // print_line("ev: "+itos(p_what)+" pnt: "+itos(p_pointer)+" pointc: "+itos(p_points.size()));
  206. switch(p_what) {
  207. case 0: { //gesture begin
  208. if (touch.size()) {
  209. //end all if exist
  210. InputEvent ev;
  211. ev.type=InputEvent::MOUSE_BUTTON;
  212. ev.ID=last_id++;
  213. ev.mouse_button.button_index=BUTTON_LEFT;
  214. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  215. ev.mouse_button.pressed=false;
  216. ev.mouse_button.x=touch[0].pos.x;
  217. ev.mouse_button.y=touch[0].pos.y;
  218. ev.mouse_button.global_x=touch[0].pos.x;
  219. ev.mouse_button.global_y=touch[0].pos.y;
  220. input->parse_input_event(ev);
  221. for(int i=0;i<touch.size();i++) {
  222. InputEvent ev;
  223. ev.type=InputEvent::SCREEN_TOUCH;
  224. ev.ID=last_id++;
  225. ev.screen_touch.index=touch[i].id;
  226. ev.screen_touch.pressed=false;
  227. ev.screen_touch.x=touch[i].pos.x;
  228. ev.screen_touch.y=touch[i].pos.y;
  229. input->parse_input_event(ev);
  230. }
  231. }
  232. touch.resize(p_points.size());
  233. for(int i=0;i<p_points.size();i++) {
  234. touch[i].id=p_points[i].id;
  235. touch[i].pos=p_points[i].pos;
  236. }
  237. {
  238. //send mouse
  239. InputEvent ev;
  240. ev.type=InputEvent::MOUSE_BUTTON;
  241. ev.ID=last_id++;
  242. ev.mouse_button.button_index=BUTTON_LEFT;
  243. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  244. ev.mouse_button.pressed=true;
  245. ev.mouse_button.x=touch[0].pos.x;
  246. ev.mouse_button.y=touch[0].pos.y;
  247. ev.mouse_button.global_x=touch[0].pos.x;
  248. ev.mouse_button.global_y=touch[0].pos.y;
  249. last_mouse=touch[0].pos;
  250. input->parse_input_event(ev);
  251. }
  252. //send touch
  253. for(int i=0;i<touch.size();i++) {
  254. InputEvent ev;
  255. ev.type=InputEvent::SCREEN_TOUCH;
  256. ev.ID=last_id++;
  257. ev.screen_touch.index=touch[i].id;
  258. ev.screen_touch.pressed=true;
  259. ev.screen_touch.x=touch[i].pos.x;
  260. ev.screen_touch.y=touch[i].pos.y;
  261. input->parse_input_event(ev);
  262. }
  263. } break;
  264. case 1: { //motion
  265. if (p_points.size()) {
  266. //send mouse, should look for point 0?
  267. InputEvent ev;
  268. ev.type=InputEvent::MOUSE_MOTION;
  269. ev.ID=last_id++;
  270. ev.mouse_motion.button_mask=BUTTON_MASK_LEFT;
  271. ev.mouse_motion.x=p_points[0].pos.x;
  272. ev.mouse_motion.y=p_points[0].pos.y;
  273. input->set_mouse_pos(Point2(ev.mouse_motion.x,ev.mouse_motion.y));
  274. ev.mouse_motion.speed_x=input->get_mouse_speed().x;
  275. ev.mouse_motion.speed_y=input->get_mouse_speed().y;
  276. ev.mouse_motion.relative_x=p_points[0].pos.x-last_mouse.x;
  277. ev.mouse_motion.relative_y=p_points[0].pos.y-last_mouse.y;
  278. last_mouse=p_points[0].pos;
  279. input->parse_input_event(ev);
  280. }
  281. ERR_FAIL_COND(touch.size()!=p_points.size());
  282. for(int i=0;i<touch.size();i++) {
  283. int idx=-1;
  284. for(int j=0;j<p_points.size();j++) {
  285. if (touch[i].id==p_points[j].id) {
  286. idx=j;
  287. break;
  288. }
  289. }
  290. ERR_CONTINUE(idx==-1);
  291. if (touch[i].pos==p_points[idx].pos)
  292. continue; //no move unncesearily
  293. InputEvent ev;
  294. ev.type=InputEvent::SCREEN_DRAG;
  295. ev.ID=last_id++;
  296. ev.screen_drag.index=touch[i].id;
  297. ev.screen_drag.x=p_points[idx].pos.x;
  298. ev.screen_drag.y=p_points[idx].pos.y;
  299. ev.screen_drag.relative_x=p_points[idx].pos.x - touch[i].pos.x;
  300. ev.screen_drag.relative_y=p_points[idx].pos.y - touch[i].pos.y;
  301. input->parse_input_event(ev);
  302. touch[i].pos=p_points[idx].pos;
  303. }
  304. } break;
  305. case 2: { //release
  306. if (touch.size()) {
  307. //end all if exist
  308. InputEvent ev;
  309. ev.type=InputEvent::MOUSE_BUTTON;
  310. ev.ID=last_id++;
  311. ev.mouse_button.button_index=BUTTON_LEFT;
  312. ev.mouse_button.button_mask=BUTTON_MASK_LEFT;
  313. ev.mouse_button.pressed=false;
  314. ev.mouse_button.x=touch[0].pos.x;
  315. ev.mouse_button.y=touch[0].pos.y;
  316. ev.mouse_button.global_x=touch[0].pos.x;
  317. ev.mouse_button.global_y=touch[0].pos.y;
  318. input->parse_input_event(ev);
  319. for(int i=0;i<touch.size();i++) {
  320. InputEvent ev;
  321. ev.type=InputEvent::SCREEN_TOUCH;
  322. ev.ID=last_id++;
  323. ev.screen_touch.index=touch[i].id;
  324. ev.screen_touch.pressed=false;
  325. ev.screen_touch.x=touch[i].pos.x;
  326. ev.screen_touch.y=touch[i].pos.y;
  327. input->parse_input_event(ev);
  328. }
  329. touch.clear();
  330. }
  331. } break;
  332. case 3: { // add tuchi
  333. ERR_FAIL_INDEX(p_pointer,p_points.size());
  334. TouchPos tp=p_points[p_pointer];
  335. touch.push_back(tp);
  336. InputEvent ev;
  337. ev.type=InputEvent::SCREEN_TOUCH;
  338. ev.ID=last_id++;
  339. ev.screen_touch.index=tp.id;
  340. ev.screen_touch.pressed=true;
  341. ev.screen_touch.x=tp.pos.x;
  342. ev.screen_touch.y=tp.pos.y;
  343. input->parse_input_event(ev);
  344. } break;
  345. case 4: {
  346. for(int i=0;i<touch.size();i++) {
  347. if (touch[i].id==p_pointer) {
  348. InputEvent ev;
  349. ev.type=InputEvent::SCREEN_TOUCH;
  350. ev.ID=last_id++;
  351. ev.screen_touch.index=touch[i].id;
  352. ev.screen_touch.pressed=false;
  353. ev.screen_touch.x=touch[i].pos.x;
  354. ev.screen_touch.y=touch[i].pos.y;
  355. input->parse_input_event(ev);
  356. touch.remove(i);
  357. i--;
  358. }
  359. }
  360. } break;
  361. }
  362. }
  363. void OS_JavaScript::process_accelerometer(const Vector3& p_accelerometer) {
  364. input->set_accelerometer(p_accelerometer);
  365. }
  366. bool OS_JavaScript::has_touchscreen_ui_hint() const {
  367. return false; //???
  368. }
  369. void OS_JavaScript::main_loop_request_quit() {
  370. if (main_loop)
  371. main_loop->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
  372. }
  373. void OS_JavaScript::set_display_size(Size2 p_size) {
  374. default_videomode.width=p_size.x;
  375. default_videomode.height=p_size.y;
  376. }
  377. void OS_JavaScript::reload_gfx() {
  378. if (gfx_init_func)
  379. gfx_init_func(gfx_init_ud,use_gl2,default_videomode.width,default_videomode.height,default_videomode.fullscreen);
  380. if (rasterizer)
  381. rasterizer->reload_vram();
  382. }
  383. Error OS_JavaScript::shell_open(String p_uri) {
  384. if (open_uri_func)
  385. return open_uri_func(p_uri)?ERR_CANT_OPEN:OK;
  386. return ERR_UNAVAILABLE;
  387. };
  388. String OS_JavaScript::get_resource_dir() const {
  389. return "/"; //javascript has it's own filesystem for resources inside the APK
  390. }
  391. String OS_JavaScript::get_locale() const {
  392. if (get_locale_func)
  393. return get_locale_func();
  394. return OS_Unix::get_locale();
  395. }
  396. String OS_JavaScript::get_data_dir() const {
  397. if (get_data_dir_func)
  398. return get_data_dir_func();
  399. return "/";
  400. //return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
  401. };
  402. 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) {
  403. default_videomode.width=800;
  404. default_videomode.height=600;
  405. default_videomode.fullscreen=true;
  406. default_videomode.resizable=false;
  407. gfx_init_func=p_gfx_init_func;
  408. gfx_init_ud=p_gfx_init_ud;
  409. main_loop=NULL;
  410. last_id=1;
  411. gl_extensions=NULL;
  412. rasterizer=NULL;
  413. open_uri_func=p_open_uri_func;
  414. get_data_dir_func=p_get_data_dir_func;
  415. get_locale_func=p_get_locale_func;
  416. }
  417. OS_JavaScript::~OS_JavaScript() {
  418. }