os_nacl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*************************************************************************/
  2. /* os_nacl.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_nacl.h"
  30. #include "drivers/unix/memory_pool_static_malloc.h"
  31. #include "os/memory_pool_dynamic_static.h"
  32. #include "main/main.h"
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. #include <sys/time.h>
  36. #include <time.h>
  37. #include "io/file_access_memory.h"
  38. #include "core/io/file_access_pack.h"
  39. #include "scene/io/scene_loader.h"
  40. #include "scene/main/scene_main_loop.h"
  41. #include "servers/visual/visual_server_raster.h"
  42. #include "drivers/gles2/rasterizer_gles2.h"
  43. #include "nacl_keycodes.h"
  44. #include "core/globals.h"
  45. #include "core/input_map.h"
  46. #include <ppapi/cpp/point.h>
  47. #include <ppapi/cpp/var.h>
  48. #define UNIX_ENABLED
  49. #include "drivers/unix/thread_posix.h"
  50. #include "drivers/unix/semaphore_posix.h"
  51. #include "drivers/unix/mutex_posix.h"
  52. int OSNacl::get_video_driver_count() const {
  53. return 1;
  54. };
  55. const char * OSNacl::get_video_driver_name(int p_driver) const {
  56. return "GLES2";
  57. };
  58. OS::VideoMode OSNacl::get_default_video_mode() const {
  59. return OS::VideoMode(800,600,false);
  60. };
  61. int OSNacl::get_audio_driver_count() const {
  62. return 1;
  63. };
  64. const char * OSNacl::get_audio_driver_name(int p_driver) const {
  65. return "nacl_audio";
  66. };
  67. static MemoryPoolStaticMalloc *mempool_static=NULL;
  68. static MemoryPoolDynamicStatic *mempool_dynamic=NULL;
  69. void OSNacl::initialize_core() {
  70. ticks_start=0;
  71. ticks_start=get_ticks_usec();
  72. };
  73. void OSNacl::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
  74. rasterizer = memnew( RasterizerGLES2 );
  75. visual_server = memnew( VisualServerRaster(rasterizer) );
  76. visual_server->init();
  77. visual_server->cursor_set_visible(false, 0);
  78. audio_driver = memnew(AudioDriverNacl);
  79. audio_driver->set_singleton();
  80. audio_driver->init();
  81. sample_manager = memnew( SampleManagerMallocSW );
  82. audio_server = memnew( AudioServerSW(sample_manager) );
  83. audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
  84. audio_server->init();
  85. spatial_sound_server = memnew( SpatialSoundServerSW );
  86. spatial_sound_server->init();
  87. spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
  88. spatial_sound_2d_server->init();
  89. //
  90. physics_server = memnew( PhysicsServerSW );
  91. physics_server->init();
  92. physics_2d_server = memnew( Physics2DServerSW );
  93. physics_2d_server->init();
  94. input = memnew(InputDefault);
  95. };
  96. void OSNacl::set_main_loop( MainLoop * p_main_loop ) {
  97. main_loop = p_main_loop;
  98. input->set_main_loop(p_main_loop);
  99. main_loop->init();
  100. };
  101. void OSNacl::delete_main_loop() {
  102. if (main_loop)
  103. memdelete(main_loop);
  104. };
  105. void OSNacl::finalize() {
  106. };
  107. void OSNacl::finalize_core() {
  108. if (mempool_dynamic)
  109. memdelete( mempool_dynamic );
  110. if (mempool_static)
  111. delete mempool_static;
  112. };
  113. void OSNacl::alert(const String& p_alert,const String& p_title) {
  114. fprintf(stderr,"ERROR: %s\n",p_alert.utf8().get_data());
  115. };
  116. void OSNacl::vprint(const char* p_format, va_list p_list, bool p_strerr) {
  117. vprintf(p_format,p_list);
  118. fflush(stdout);
  119. }
  120. String OSNacl::get_stdin_string(bool p_block) {
  121. char buff[1024];
  122. return fgets(buff,1024,stdin);
  123. };
  124. void OSNacl::set_mouse_show(bool p_show) {
  125. };
  126. void OSNacl::set_mouse_grab(bool p_grab) {
  127. };
  128. bool OSNacl::is_mouse_grab_enabled() const {
  129. return false;
  130. };
  131. int OSNacl::get_mouse_button_state() const {
  132. return mouse_mask;
  133. };
  134. Point2 OSNacl::get_mouse_pos() const {
  135. return Point2();
  136. };
  137. void OSNacl::set_window_title(const String& p_title) {
  138. };
  139. void OSNacl::set_video_mode(const VideoMode& p_video_mode, int p_screen) {
  140. video_mode = p_video_mode;
  141. };
  142. OS::VideoMode OSNacl::get_video_mode(int p_screen) const {
  143. return video_mode;
  144. };
  145. void OSNacl::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
  146. };
  147. Error OSNacl::execute(const String& p_path, const List<String>& p_arguments,bool p_blocking, OS::ProcessID *r_child_id, String* r_pipe, int *r_exitcode) {
  148. return ERR_UNAVAILABLE;
  149. };
  150. Error OSNacl::kill(const ProcessID& p_pid) {
  151. return ERR_UNAVAILABLE;
  152. };
  153. bool OSNacl::has_environment(const String& p_var) const {
  154. return getenv(p_var.utf8().get_data())!=NULL;
  155. };
  156. String OSNacl::get_environment(const String& p_var) const {
  157. if (getenv(p_var.utf8().get_data()))
  158. return getenv(p_var.utf8().get_data());
  159. return "";
  160. };
  161. String OSNacl::get_name() {
  162. return "NaCl";
  163. };
  164. MainLoop *OSNacl::get_main_loop() const {
  165. return main_loop;
  166. };
  167. OS::Date OSNacl::get_date() const {
  168. time_t t=time(NULL);
  169. struct tm *lt=localtime(&t);
  170. Date ret;
  171. ret.year=lt->tm_year;
  172. ret.month=(Month)lt->tm_mon;
  173. ret.day=lt->tm_mday;
  174. ret.weekday=(Weekday)lt->tm_wday;
  175. ret.dst=lt->tm_isdst;
  176. return ret;
  177. };
  178. OS::Time OSNacl::get_time() const {
  179. time_t t=time(NULL);
  180. struct tm *lt=localtime(&t);
  181. Time ret;
  182. ret.hour=lt->tm_hour;
  183. ret.min=lt->tm_min;
  184. ret.sec=lt->tm_sec;
  185. return ret;
  186. };
  187. void OSNacl::delay_usec(uint32_t p_usec) const {
  188. //usleep(p_usec);
  189. };
  190. uint64_t OSNacl::get_ticks_usec() const {
  191. struct timeval tv_now;
  192. gettimeofday(&tv_now,NULL);
  193. uint64_t longtime = (uint64_t)tv_now.tv_usec + (uint64_t)tv_now.tv_sec*1000000L;
  194. longtime-=ticks_start;
  195. return longtime;
  196. };
  197. bool OSNacl::can_draw() const {
  198. return minimized != true;
  199. };
  200. void OSNacl::queue_event(const InputEvent& p_event) {
  201. ERR_FAIL_INDEX( event_count, MAX_EVENTS );
  202. event_queue[event_count++] = p_event;
  203. };
  204. void OSNacl::add_package(String p_name, Vector<uint8_t> p_data) {
  205. FileAccessMemory::register_file(p_name, p_data);
  206. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_RESOURCES);
  207. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_USERDATA);
  208. FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_FILESYSTEM);
  209. if (!PackedData::get_singleton())
  210. memnew(PackedData);
  211. printf("adding package %ls, %x\n", p_name.c_str(), PackedData::get_singleton());
  212. PackedData::get_singleton()->set_disabled(true);
  213. PackedData::get_singleton()->add_pack(p_name);
  214. PackedData::get_singleton()->set_disabled(false);
  215. printf("added\n");
  216. };
  217. void OSNacl::set_cursor_shape(CursorShape p_shape) {
  218. };
  219. String OSNacl::get_resource_dir() const {
  220. return ".";
  221. };
  222. static int mouse_button(int p_nacl_but) {
  223. switch (p_nacl_but) {
  224. case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
  225. return BUTTON_LEFT;
  226. case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
  227. return BUTTON_MIDDLE;
  228. case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
  229. return BUTTON_RIGHT;
  230. };
  231. return 0;
  232. };
  233. static InputModifierState modifier(uint32_t p_mod) {
  234. InputModifierState mod_mask;
  235. mod_mask.shift = p_mod & PP_INPUTEVENT_MODIFIER_SHIFTKEY;
  236. mod_mask.alt = p_mod & PP_INPUTEVENT_MODIFIER_ALTKEY;
  237. mod_mask.control = p_mod & PP_INPUTEVENT_MODIFIER_CONTROLKEY;
  238. mod_mask.meta = p_mod & PP_INPUTEVENT_MODIFIER_METAKEY;
  239. return mod_mask;
  240. };
  241. void OSNacl::handle_event(const pp::InputEvent& p_event) {
  242. int type = p_event.GetType();
  243. switch (type) {
  244. case PP_INPUTEVENT_TYPE_MOUSEDOWN:
  245. case PP_INPUTEVENT_TYPE_MOUSEUP:
  246. case PP_INPUTEVENT_TYPE_WHEEL: {
  247. InputEvent event;
  248. event.ID=++event_id;
  249. event.type = InputEvent::MOUSE_BUTTON;
  250. event.device=0;
  251. pp::MouseInputEvent mevent(p_event);
  252. if (type == PP_INPUTEVENT_TYPE_WHEEL) {
  253. pp::WheelInputEvent wevent(p_event);;
  254. float ticks = wevent.GetTicks().y();
  255. if (ticks == 0)
  256. break; // whut?
  257. event.mouse_button.pressed = true;
  258. event.mouse_button.button_index = ticks > 0 ? BUTTON_WHEEL_UP : BUTTON_WHEEL_DOWN;
  259. event.mouse_button.doubleclick = false;
  260. } else {
  261. event.mouse_button.pressed = (type == PP_INPUTEVENT_TYPE_MOUSEDOWN);
  262. event.mouse_button.button_index = mouse_button(mevent.GetButton());
  263. event.mouse_button.doubleclick = (mevent.GetClickCount() % 2) == 0;
  264. mouse_mask &= ~(1<< (event.mouse_button.button_index - 1));
  265. mouse_mask |= (event.mouse_button.pressed << (event.mouse_button.button_index - 1));
  266. };
  267. pp::Point pos = mevent.GetPosition();
  268. event.mouse_button.button_mask = mouse_mask;
  269. event.mouse_button.global_x = pos.x();
  270. event.mouse_button.x = pos.x();
  271. event.mouse_button.global_y = pos.y();
  272. event.mouse_button.y = pos.y();
  273. event.mouse_button.pointer_index = 0;
  274. event.mouse_button.mod = modifier(p_event.GetModifiers());
  275. queue_event(event);
  276. } break;
  277. case PP_INPUTEVENT_TYPE_MOUSEMOVE: {
  278. pp::MouseInputEvent mevent(p_event);
  279. pp::Point pos = mevent.GetPosition();
  280. InputEvent event;
  281. event.ID=++event_id;
  282. event.type = InputEvent::MOUSE_MOTION;
  283. event.mouse_motion.pointer_index = 0;
  284. event.mouse_motion.global_x = pos.x();
  285. event.mouse_motion.global_y = pos.y();
  286. event.mouse_motion.x = pos.x();
  287. event.mouse_motion.y = pos.y();
  288. event.mouse_motion.button_mask = mouse_mask;
  289. event.mouse_motion.mod = modifier(p_event.GetModifiers());
  290. event.mouse_motion.relative_x = pos.x() - mouse_last_x;
  291. event.mouse_motion.relative_y = pos.y() - mouse_last_y;
  292. mouse_last_x = pos.x();
  293. mouse_last_y = pos.y();
  294. queue_event(event);
  295. } break;
  296. case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
  297. case PP_INPUTEVENT_TYPE_KEYDOWN:
  298. case PP_INPUTEVENT_TYPE_KEYUP: {
  299. pp::KeyboardInputEvent kevent(p_event);
  300. bool is_char;
  301. uint32_t key = godot_key(kevent.GetKeyCode(), is_char);
  302. if (type != PP_INPUTEVENT_TYPE_KEYUP && is_char) {
  303. last_scancode = key;
  304. break;
  305. };
  306. InputEvent event;
  307. event.ID=++event_id;
  308. event.type = InputEvent::KEY;
  309. event.key.pressed = (type != PP_INPUTEVENT_TYPE_KEYUP);
  310. event.key.scancode = key;
  311. event.key.unicode = key;
  312. event.key.echo = p_event.GetModifiers() & PP_INPUTEVENT_MODIFIER_ISAUTOREPEAT;
  313. event.key.mod = modifier(p_event.GetModifiers());
  314. queue_event(event);
  315. } break;
  316. case PP_INPUTEVENT_TYPE_CHAR: {
  317. pp::KeyboardInputEvent kevent(p_event);
  318. InputEvent event;
  319. event.ID = ++event_id;
  320. event.type = InputEvent::KEY;
  321. event.key.pressed = true;
  322. event.key.scancode = last_scancode;
  323. event.key.unicode = kevent.GetCharacterText().AsString().c_str()[0];
  324. event.key.mod = modifier(p_event.GetModifiers());
  325. event.key.echo = p_event.GetModifiers() & PP_INPUTEVENT_MODIFIER_ISAUTOREPEAT;
  326. queue_event(event);
  327. } break;
  328. /*
  329. case NPEventType_Minimize: {
  330. minimized = p_event->u.minimize.value == 1;
  331. } break;
  332. case NPEventType_Focus: {
  333. if (p_event->u.focus.value == 1) {
  334. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
  335. } else {
  336. main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
  337. };
  338. } break;
  339. */
  340. default:
  341. ;
  342. };
  343. };
  344. bool OSNacl::iterate() {
  345. if (!main_loop) {
  346. event_count = 0;
  347. return true;
  348. };
  349. for (int i=0; i<event_count; i++) {
  350. input->parse_input_event(event_queue[i]);
  351. };
  352. event_count = 0;
  353. return Main::iteration();
  354. };
  355. OSNacl::OSNacl() {
  356. main_loop=NULL;
  357. mempool_dynamic = NULL;
  358. mempool_static = NULL;
  359. mouse_last_x = 0;
  360. mouse_last_y = 0;
  361. event_count = 0;
  362. event_id = 0;
  363. mouse_mask = 0;
  364. video_mode = get_default_video_mode();
  365. last_scancode = 0;
  366. minimized = false;
  367. ThreadPosix::make_default();
  368. SemaphorePosix::make_default();
  369. MutexPosix::make_default();
  370. mempool_static = new MemoryPoolStaticMalloc;
  371. mempool_dynamic = memnew( MemoryPoolDynamicStatic );
  372. };
  373. OSNacl::~OSNacl() {
  374. };